PHP:如何跟踪缺少输入数据的超时?

问题描述 投票:1回答:1

有一些代码可以接收来自STDIN的输入行:

#!/usr/bin/php
<?php
while (false !== ($line = fgets(STDIN))) {
      if (preg_match('/start/',$line)) {
         echo $line , "\n";
      }
}
?>

我的问题是:如何在1分钟内跟踪缺少输入数据的超时并告知是否万一?

php stdin
1个回答
2
投票

我从这里[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";
    }
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.