Home >Backend Development >PHP Problem >How to solve the php exec garbled problem
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.
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);
The cmd.py file output contains Chinese, and the result is displayed As shown in the picture below:
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);
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);
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!