Bug #72019 [Opn->Nab]: stdin fgets substr
Edit report at https://bugs.php.net/bug.php?id=72019&edit=1
ID: 72019
Updated by: jhdxr@php.net
Reported by: k6dg at outlook dot com
Summary: stdin fgets substr
-Status: Open
+Status: Not a bug
Type: Bug
Package: CGI/CLI related
Operating System: windows7
PHP Version: 7.0.5
Block user comment: N
Private report: N
New Comment:
first, I think you confused expected result and actual result. "12hello" should be the
expected result while "hello" is printed.
In fact, php works as expected. When you type "12<enter> in windows, the string actually
is "12\r\n", and result of substr($test, 0, -1) is "12\r".
"\r" means moving cursor to the begining of the line, and if you continue print something,
they will cover the origin display. you can try stdout('a') instead of
stdout('hello') and you will see "a2", because "a" covers
"1".
If you want to remove the line break in the output, trim or substr($in, 0,
-strlen(PHP_EOL)) may be a better choice.
Previous Comments:
------------------------------------------------------------------------
[2016-04-13 17:04:59] k6dg at outlook dot com
Description:
------------
it only appear in the ZTS version.
use php://stdin get a line with '\n'.
use substr cutoff last char and return it.
use php://stdout print the return value, and print something else.
the return value can not be seen.
Test script:
---------------
function stdin () {
$stdin = fopen('php://stdin','r');
$text = fgets($stdin); // This
fclose($stdin);
return substr($text, 0, -1); // and This couse the problem
}
function stdout(string $data) {
$stdout = fopen('php://stdout','a');
$length = fwrite($stdout, $data);
fclose($stdout);
// $length = file_put_contents ('php://stdout', $data, FILE_APPEND);
return $length;
}
stdout(stdin());
stdout('hello');
Expected result:
----------------
input:
12
output:
hello
Actual result:
--------------
input:
12
output:
12hello
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72019&edit=1
Thread (3 messages)