Skip to content

Instantly share code, notes, and snippets.

@noname007
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noname007/b8997cc13a71a1276fe8 to your computer and use it in GitHub Desktop.
Save noname007/b8997cc13a71a1276fe8 to your computer and use it in GitHub Desktop.
程序运行时从命令行读取数据. read data from cmd while running.
<?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