note 78672 modified in features.commandline by james

From: Date: Mon, 22 Oct 2007 23:16:04 +0000
Subject: note 78672 modified in features.commandline by james
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-131649@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. --was-- 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. http://php.net/manual/en/features.commandline.php

« previous php.notes (#131649) next »