How to solve the php exec garbled problem

藏色散人
Release: 2023-03-14 17:24:02
Original
3138 people have browsed it

Solution to php exec garbled code: 1. Execute "exec("python cmd.py",$str);"; 2. Pass "iconv("GBK", "UTF-8", $res );" method to convert the encoding.

How to solve the php exec garbled problem

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to solve the php exec garbled problem?

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 As shown in the picture below:

How to solve the php exec garbled problem

Many bloggers say that it can be solved by setting a predetermined encoding. For example, add the encoding before executing the command and then execute it:

$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;
shell_exec:
$res = shell_exec("python cmd.py");
echo iconv("GBK", "UTF-8", $res);
Copy after login

Both the exec and shell_exec returns above can display Chinese!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the php exec garbled problem. 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