note 91643 modified in function.readline by danbrown

From: Date: Sun, 21 Jun 2009 15:12:22 +0000
Subject: note 91643 modified in function.readline by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-157195@lists.php.net to get a copy of this message
I wanted a function that would timeout if readline was waiting too long... this works on php CLI on linux: <?php function readline_timeout($sec, $def) { return trim(shell_exec('bash -c ' . escapeshellarg('phprlto=' . escapeshellarg($def) . ';' . 'read -t ' . ((int)$sec) . ' phprlto;' . 'echo "$phprlto"'))); } ?> Just call readline_timeout(5, 'whatever') to either read something from stdin, or timeout in 5 seconds and default to 'whatever'. I tried just using shell_exec without relying on bash -c, but that didn't work for me, so I had to go the round about way. --was-- I wanted a function that would timeout if readline was waiting too long... this works on php CLI on linux: <? function readline_timeout($sec, $def) { return trim(shell_exec('bash -c ' . escapeshellarg('phprlto=' . escapeshellarg($def) . ';' . 'read -t ' . ((int)$sec) . ' phprlto;' . 'echo "$phprlto"'))); } ?> Just call readline_timeout(5, 'whatever') to either read something from stdin, or timeout in 5 seconds and default to 'whatever'. I tried just using shell_exec without relying on bash -c, but that didn't work for me, so I had to go the round about way. http://php.net/manual/en/function.readline.php

« previous php.notes (#157195) next »