Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow keys not working in php CLI script | EDIT: STDIN involved

i recently entered the fascinating world of PHP CLI.

I run then the script on an ubuntu terminal and when i pressed the arrows keys, instead of moving the cursor, it printed the escape codes ^[[A^[[B^[[C^[[D.

If i use the normal terminal the problem doesn't appear. So i guess that into my script something prevents the normal behaviour.

Does someone have any solution?

Thanks for the help!

EDIT:

Looks like it's STDIN, which I use to catch the inputs, the problem! When waiting for STDIN inputs, the terminal shows the wrong characters!! How can I solve this?

like image 599
Bakaburg Avatar asked Feb 26 '26 03:02

Bakaburg


1 Answers

I suggest replacing the fgets(STDIN) call with the readline() function, which is available at least in Linux environments. Note that the readline library is not shipped in OS X, although it can be installed separately.

// $input = fgets(STDIN);
$input = readline('Input: ');

If the only reason you need line editing is removing the surplus ´-characters from a drag and dropped file path, you can always trim any extra characters from the string in your script.

$input = readline('Input: ');
$input = trim($input, '´');
like image 137
Viktor Avatar answered Feb 27 '26 18:02

Viktor