menu

arrow_back Why it displays gibberish?

by
0 votes
I use PHP under Windows 10 code
<?php
$data = 'help';
exec(escapeshellcmd($data), $output, $return);
echo implode("\n", $output)."\nReturn Code: $return";
?>
and he's in a browser displays:
5adc98b51852f885742916.png

Почему так происходит??

2 Answers

by
 
Best answer
0 votes
If you are using a server with PHP 5.6 and above, then you need to send the browser an http header, the encoding in which the script is to give data independence in what encoding the script file, and, even if you normally give HTML document structure.
//установка кодировки вывода
header('Content-Type: text/html; charset=utf-8');

Если не знаете, в какой кодировке будут данные, то нужно идентифицировать кодировку, и вписать ее в header():
echo mb_detect_encoding(implode("\n", $output));
----------------------------
Кодировку подобрал для заголовка методом тыка, так как mb_detect_encoding() не вывел явного ответа.
У меня оказалась CP866:
header('Content-Type: text/html; charset=CP866');
$data = 'help';
exec(escapeshellcmd($data), $output, $return);
echo implode("<br>", $output)."<br>Return Code: ".$return;
by
0 votes
and if so...
echo  iconv('UTF-8', 'windows-1251//TRANSLIT', implode("\n", $output))."\nReturn Code: $return";

2 Comments

well... right?
echo  @iconv('UTF-8', 'windows-1251//IGNORE', implode("\n", $output))."\nReturn Code: $return";
Notice: iconv() [function.iconv.php]: Detected an illegal character in input string
That's what gave me