note 19129 added to function.readline
| From: | don at undesigned dot org dot za | Date: | Sun, 17 Feb 2002 02:46:22 +0000 |
| Subject: | note 19129 added to function.readline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26708@lists.php.net to get a copy of this message | ||
This might help some of you a bit:
/* example for how to return console colours - same as normal console colours with chr(27) for ^[ -
returns 0 to reset if null */
function cl($str = 0) {
return (chr(27) . "[" . $str . "m");
}
/* check user input for readline() (uses $default if given), asks for it again if user input is null
and $default not specified. */
function readline_add($str,$default = null) {
for ($i = 0; $i < 1; $i++) {
if ($default !== null) {
$line = readline(cl("1;31") . $str . cl("1;37") . " [$default]:
");
if (isset($line) && $line !== "") {
readline_add_history($line); /* add history */
} else {
readline_add_history($default); /* add default to history */
}
} else {
$line = readline(cl("1;31") . $str . cl("1;37") . ": ");
if (isset($line) && $line !== "") {
readline_add_history($line); /* add history */
} else {
$i -= 1; /* we -1 to repeat the query if input is null */
}
}
}
}
readline_add("one"); /* $history[0] - if no input is given, it asks for it again */
readline_add("two","bla"); /* $history[1] - if no input is given it defaults to
"bla" */
/* readline_list_history() returns an array so handle it how you wish */
$history = readline_list_history();
print "One: " . $history[0] . "\n";
print "Two: " . $history[1] . "\n";
print cl(); /* reset colours with 0 */
--
http://www.php.net/manual/en/function.readline.php
http://master.php.net/manage/user-notes.php?action=edit+19129
http://master.php.net/manage/user-notes.php?action=delete+19129
http://master.php.net/manage/user-notes.php?action=reject+19129