有從 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 關注
- 117 瀏覽
添加回答
舉報
0/150
提交
取消
