【发布时间】:2011-04-10 16:58:00
【问题描述】:
我想从 PHP 中的命令行一次读取一个字符,但似乎有某种输入缓冲从某处阻止了这一点。
考虑这段代码:
#!/usr/bin/php
<?php
echo "input# ";
while ($c = fread(STDIN, 1)) {
echo "Read from STDIN: " . $c . "\ninput# ";
}
?>
输入“foo”作为输入(并按回车键),我得到的输出是:
input# foo
Read from STDIN: f
input# Read from STDIN: o
input# Read from STDIN: o
input# Read from STDIN:
input#
我期待的输出是:
input# f
input# Read from STDIN: f
input# o
input# Read from STDIN: o
input# o
input# Read from STDIN: o
input#
input# Read from STDIN:
input#
(也就是说,字符在输入时被读取和处理)。
然而,目前,每个字符只有在按下回车后才会被读取。我怀疑 TTY 正在缓冲输入。
最终我希望能够阅读诸如向上箭头、向下箭头等按键。
【问题讨论】:
标签: php stdin command-line-interface buffering tty