【问题标题】:PHP: How to track the timeout of the absence of input data?PHP:如何跟踪没有输入数据的超时?
【发布时间】:2020-04-15 08:17:38
【问题描述】:

有从 STDIN 接收输入行的代码:

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

我的问题是:如何跟踪1分钟没有输入数据的超时并告知是否以防万一?

【问题讨论】:

    标签: php stdin


    【解决方案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";
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      相关资源
      最近更新 更多