In PHP, I can read in input from a command line program with the following code
$stream = STDIN;
$test = fgets($stream);
echo $test;
This works well for simple input. However, if I try to use something like an back arrow key, my shell looks like the following
This is a test^[[D^[[D^[[D
i.e., the arrow key escape sequence of ^[[D
is sent to the shell. PHP itself will interpret the arrow keys -- i.e. inputing this
This is a test^[[D^[[D^[[D^[[Dsecond test
will output this
This is a second test
However, I'd like the shell to "correctly" (i.e. do what I think they should do, not literally what I sent) interpret the arrow keys so the insertion point moves while typing.
Is this possible in PHP? With an extension? Without an extension? I've tried variations of fgets($stream, 1)
but it seems like PHP just hangs until the user inputs an enter key.
A bit tricky but found a way using the mbstring library:
// stream_set_blocking(STDIN, false); // Do not wait
while ($c = fread(STDIN, 16)) {
$c = preg_replace('/[^[:print:]\n]/u', '', mb_convert_encoding($c, 'UTF-8', 'UTF-8'));
/* Up arrow */
if ($c === "[A"){
echo "UP";
}
/* Down arrow */
if ($c === "[B"){
echo "DOWN";
}
/* Right arrow */
if ($c === "[C"){
echo "RIGHT";
}
/* LEFT arrow */
if ($c === "[D"){
echo "LEFT";
}
}
Hide the shell character printing:
system("stty -echo");
Restore at script end:
stty echo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With