有一些代码可以接收来自STDIN的输入行:
#!/usr/bin/php
<?php
while (false !== ($line = fgets(STDIN))) {
if (preg_match('/start/',$line)) {
echo $line , "\n";
}
}
?>
我的问题是:如何在1分钟内跟踪缺少输入数据的超时并告知是否万一?
我从这里[PHP CLI - Ask for User Input or Perform Action after a Period of Time开始使用hek2mgl的答案解决了我的问题,>
这是我的代码:
#!/usr/bin/php
<?php
echo "input something ... (5 sec)\n";
$stdin = fopen('php://stdin', 'r');
while(true) {
$read = array($stdin);
$write = $except = array();
$timeout = 5;
if(stream_select($read, $write, $except, $timeout)) {
$line = fgets($stdin);
if (preg_match('/start/',$line)) {
echo $line , "\n";
}
} else {
echo "you typed nothing\n";
}
}
?>