Bug #72019 [Nab]: stdin fgets substr
| From: | k6dg at outlook dot com | Date: | Thu, 14 Apr 2016 03:47:12 +0000 |
| Subject: | Bug #72019 [Nab]: stdin fgets substr | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-200540@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72019&edit=1
ID: 72019
User updated by: k6dg at outlook dot com
Reported by: k6dg at outlook dot com
Summary: stdin fgets substr
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:
ZTS version and NTS version get difference result
Previous Comments:
------------------------------------------------------------------------
[2016-04-14 02:47:36] jhdxr@php.net
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.
------------------------------------------------------------------------
[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