0
votes

Can't set correct encoding to outptut text buffer of the console process at russian windows 10. Please help with it. Need to convert console output into UTF-8 encoding and output in browser.

I'm tried different encodings:

  • utf-16 -> utf-8
  • ascii -> utf-8
  • windows-1251 -> utf-8
  • windows-1252 -> utf-8

But no luck :(

It's output only english characters and numbers. No russian symbols

<?php

header('Content-Type: text/html; charset=utf-8');

$cmd = "ping mail.ru";

execAndOutput($cmd);

function execAndOutput($cmd){
    $process = popen($cmd, 'r'); 

    while($out = fgets($process, 2048)) { 
        $out = mb_convert_encoding($out, "utf-8", "windows-1251");
        echo $out."<br>\n"; 
        ob_flush();flush(); 
    } 
    pclose($process); 
} ?>

With encoding 1251 I've got this in output:

ЋЎ¬Ґ­ Ї ЄҐв ¬Ё б mail.ru [94.100.180.202] б 32 Ў ©в ¬Ё ¤ ­­ле: 
ЋвўҐв ®в 94.100.180.202: зЁб«® Ў ©в=32 ўаҐ¬п=118¬б TTL=49 
ЋвўҐв ®в 94.100.180.202: зЁб«® Ў ©в=32 ўаҐ¬п=118¬б TTL=49 

‘в вЁбвЁЄ  Ping ¤«п 94.100.180.202: 
Џ ЄҐв®ў: ®вЇа ў«Ґ­® = 4, Ї®«г祭® = 4, Ї®вҐап­® = 0 
(0% Ї®вҐам) 
ЏаЁЎ«Ё§ЁвҐ«м­®Ґ ўаҐ¬п ЇаЁҐ¬ -ЇҐаҐ¤ зЁ ў ¬б: 
ЊЁ­Ё¬ «м­®Ґ = 118¬бҐЄ, Њ ЄбЁ¬ «м­®Ґ = 120 ¬бҐЄ, ‘।­ҐҐ = 118 ¬бҐЄ 

Without any encoding convertion I've got this:

����� ����⠬� � mail.ru [94.100.180.200] � 32 ���⠬� ������: 
�⢥� �� 94.100.180.200: �᫮ ����=32 �६�=113�� TTL=46 
�⢥� �� 94.100.180.200: �᫮ ����=32 �६�=170�� TTL=46 

����⨪� Ping ��� 94.100.180.200: 
����⮢: ��ࠢ���� = 4, ����祭� = 4, ����ﭮ = 0 
(0% �����) 
�ਡ����⥫쭮� �६� �ਥ��-��।�� � ��: 
�������쭮� = 109�ᥪ, ���ᨬ��쭮� = 170 �ᥪ, �।��� = 135 �ᥪ

Expected (copied from windows terminal):

Обмен пакетами с 127.0.0.1 по с 32 байтами данных:
Ответ от 127.0.0.1: число байт=32 время<1мс TTL=128
Ответ от 127.0.0.1: число байт=32 время<1мс TTL=128

Статистика Ping для 127.0.0.1:
    Пакетов: отправлено = 4, получено = 4, потеряно = 0
    (0% потерь)
Приблизительное время приема-передачи в мс:
    Минимальное = 0мсек, Максимальное = 0 мсек, Среднее = 0 мсек

I'm want to get Russian symbols from console in browser.

2
Do you get any output for the russian characters, such as questionmarks or tofu rectangles? Can you include an example, please? - Dragonthoughts
Have a try using $out = mb_convert_encoding($out, "UTF-8", "ISO-8859-5"); - arkascha
@Dragonthoughts. I'm added examples of output into my question - Levon Tumasov
@arkascha. I'm tried your version but no luck others strange symbols :( - Levon Tumasov
Try simplifying the problem. Potentially you could be corrupting the data on reading or writing. If you eliminate reading from a file, but output a string that you create in code, how does your program behave? - Dragonthoughts

2 Answers

0
votes

you can use iconv function

iconv('from_encoding', 'to_encoding',$str);

also you can correct your encoding like this

iconv('utf-8', 'utf-8//IGNORE',$str);
0
votes

I'm fonded solution for this.

Console settings

In windows console settings i found the current encoding is CP866 and add it to mb_encoding.

For Russian symbols in console:

mb_convert_encoding($out, 'utf-8', 'cp866');

or

 iconv('cp866', 'utf-8', $out);

Check your console settings to figuraute what encoding it use.

Now all works like a sharm. Thanks a lot for the help!