How to read non-ASCII characters from CLI standard input

为君一笑 提交于 2019-12-02 20:44:57

Possible solution:

echo '>'; 
$line = stream_get_line(STDIN, 999999, PHP_EOL);

Notes: I was unable to reproduce your error using multiple versions of PHP. Using the following PHP version 5.3.8 gave me no issues

PHP 5.3 (5.3.8) VC9 x86 Non Thread Safe (2011-Aug-23 12:26:18) Arcitechture is Win XP SP3 32 bit

You might try upgrading PHP.

I downloaded php-5.3.5-nts-Win32-VC6-x86 and was not able to reproduce your error, it works fine for me.

Edit: Additionaly I typed the characters using my spanish keyboard.

Edit2:

CMD Command:

chcp 437

PHP Code:

<?php
$fp=fopen("php://stdin","r");
while(1){
    $str =  fgets(STDIN);
    echo mb_detect_encoding($str)."\n";
    echo '>'.stream_get_line($fp,999999,"\n")."\n";
}
?>

Output:

test
ASCII
test
>test
öïü

öïü
>öïü

I think that happens because PHP 5.3 does not support properly multibyte characters.

These chars: ÅÄÖåäö

Are binary: c3 85 c3 84 c3 96 c3 a5 c3 a4 c3 b6 (without BOM at beggining)

Citing PHP String:

A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. See details of the string type.

Normally does not affect the final result, because the browser/reader understand multibyte characters, but for CMD and STDIN buffer is ÅÄÖåäö (12 chars/bytes char array).

only MB functions handle multibyte strings basic operations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!