Home >Backend Development >PHP Problem >What to do if php exec results are garbled
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;".
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);
The cmd.py file output contains Chinese, and the result is displayed It is shown in the picture below:
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);
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);
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!