note 78672 added to features.commandline
| From: | james_s2010 at NOSPAM dot hotmail dot com | Date: | Mon, 22 Oct 2007 21:11:45 +0000 |
| Subject: | note 78672 added to features.commandline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131644@lists.php.net to get a copy of this message | ||
I was looking for a way to interactively get a single character response from user. Using STDIN with
fread, fgets and such will only work after pressing enter. So I came up with this instead:
#!/usr/bin/php -q
<?php
function inKey($vals) {
$inKey = "";
While(!in_array($inKey,$vals)) {
$inKey = trim(
read -s -n1 valu;echo \$valu);
}
return $inKey;
}
function echoAT($Row,$Col,$prompt="") {
// Display prompt at specific screen coords
echo "\033[".$Row.";".$Col."H".$prompt;
}
// Display prompt at position 10,10
echoAT(10,10,"Opt : ");
// Define acceptable responses
$options = array("1","2","3","4","X");
// Get user response
$key = inKey($options);
// Display user response & exit
echoAT(12,10,"Pressed : $key\n);
?>
Hope this helps someone.
----
Server IP: 208.69.120.35
Probable Submitter: 12.192.120.19
----
Manual Page -- http://www.php.net/manual/en/features.commandline.php
Edit -- https://master.php.net/note/edit/78672
Del: integrated -- https://master.php.net/note/delete/78672/integrated
Del: useless -- https://master.php.net/note/delete/78672/useless
Del: bad code -- https://master.php.net/note/delete/78672/bad+code
Del: spam -- https://master.php.net/note/delete/78672/spam
Del: non-english -- https://master.php.net/note/delete/78672/non-english
Del: in docs -- https://master.php.net/note/delete/78672/in+docs
Del: other reasons-- https://master.php.net/note/delete/78672
Reject -- https://master.php.net/note/reject/78672
Search -- https://master.php.net/manage/user-notes.php