Last active
August 29, 2015 14:19
-
-
Save noname007/b8997cc13a71a1276fe8 to your computer and use it in GitHub Desktop.
程序运行时从命令行读取数据. read data from cmd while running.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function read_cmd_input($rule_read_cmd = array()) | |
{ | |
if(empty($rule_read_cmd)) | |
$rule_read_cmd = array('yes'=>1,'no'=>0);/*结束读取的操作,及对应的返回值*/ | |
$cmd = ''; | |
$fh = fopen('php://stdin','r'); | |
$rule_read_cmd_key = array_keys($rule_read_cmd); | |
$rule_read_cmd_str = join(',',$rule_read_cmd_key); | |
echo 'Please input [',$rule_read_cmd_str,']:'; | |
while(!in_array($cmd,$rule_read_cmd,1)){ | |
$cmd = rtrim(strtolower(fgets($fh))); | |
if(!isset($rule_read_cmd[$cmd])) | |
{ | |
echo 'Please input [',$rule_read_cmd_str,']:'; | |
}else{ | |
return $rule_read_cmd[$cmd]; | |
} | |
} | |
fclose($fh); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment