有从 STDIN 接收输入行的代码:#!/usr/bin/php<?phpwhile (false !== ($line = fgets(STDIN))) { if (preg_match('/start/',$line)) { echo $line , "\n"; }}?>我的问题是:如何跟踪没有输入数据1分钟的超时时间,并在万一的情况下通知?
1 回答
森栏
TA贡献1810条经验 获得超5个赞
我从这里使用 hek2mgl 的答案解决了我的问题 [ PHP CLI - Ask for User Input or Perform Action after a Period of Time
这是我的代码:
#!/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";
}
}
?>
- 1 回答
- 0 关注
- 150 浏览
添加回答
举报
0/150
提交
取消
