What to do if php exec results are garbled

藏色散人
Release: 2023-03-17 08:00:02
Original
1382 people have browsed it

Solution to garbled php exec results: 1. Open the corresponding PHP code file; 2. Check the code at exec; 3. Modify the content to "exec("python cmd.py",$str); foreach($str as $res){$str = iconv("GBK", "UTF-8", $res);}echo $str;".

What to do if php exec results are garbled

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

What should I do if the php exec result is garbled?

php Fill in the pit exec or shell_exec cannot return Chinese garbled characters

Originally wanted to execute:

exec("python cmd.py",$str);
var_dump($str);
Copy after login

The cmd.py file output contains Chinese, and the result is displayed It is shown in the picture below:

What to do if php exec results are garbled

After searching on Baidu and csdn, many bloggers said that it can be solved by setting a predetermined encoding. For example, add the encoding before executing the command and then execute:

$locale='en_US.UTF-8';
setlocale(LC_ALL,$locale);
putenv('LC_ALL='.$locale);
exec("python cmd.py",$str);
var_dump($str);
Copy after login

After trying it, I found that the problem still exists and it has no effect. . After a long search on Google, I finally found the solution, so I didn't dare to leave it alone, so I posted it to share. . No nonsense, just go straight to it. . .

exec:

exec("python cmd.py",$str);
foreach($str as $res)
{
    $str = iconv("GBK", "UTF-8", $res);
}
echo $str;
Copy after login

shell_exec:

$res = shell_exec("python cmd.py");
echo iconv("GBK", "UTF-8", $res);
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of What to do if php exec results are garbled. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template