It may be a bit late, but here is a solution that solved this problem for me: In the C # Console
style, I wrote a small class that can readLine()
, as well as writeLine($str)
:
class Console { const READLINE_MAX_LENGTH = 0xFFFF; const WRITELINE_NEWLINE = "\n"; private static $stdin; private static $stdout; public static function /*void*/ close () { fclose(self::$stdin); fclose(self::$stdout); } public static function /*void*/ open () { self::$stdin = fopen('php://stdin', 'r'); self::$stdout = fopen('php://stdout', 'w'); } public static function /*string*/ readLine () { return stream_get_line(self::$stdin, self::READLINE_MAX_LENGTH, "\r\n"); } public static function /*void*/ writeLine ( $str) { fwrite(self::$stdout, $str); fwrite(self::$stdout, self::WRITELINE_NEWLINE); } }
Usage example:
Console::open(); echo "Input something: "; $str = Console::readLine(); if (is_string($str)) Console::writeLine($str); else echo "ERROR"; Console::close();
EDIT: This method obviously only works if the parent process does not modify STDOUT or STDIN.
source share