shell_exec()的输出包含重音的字符 [英] Output from shell_exec() containing accented charcters getting mangled
问题描述
我有一个命令,我使用 shell_exec()
。
有时,命令的输出将包含重音字符。
I've got a command which I'm running from PHP using shell_exec()
.
Sometimes the output of the command will contain accented characters.
从Bash运行时,输出会正确显示。但是,从 shell_exec
运行时,重音字符会丢失,并且输出会被截断。
When run from Bash, the output appears correctly. However, when run from shell_exec
, the accented characters are lost and the output truncated somewhat.
code> Bash :
Example output from Bash
:
. D 0 Tue Oct 25 16:45:26 2011
.. D 0 Tue Oct 25 16:45:26 2011
...
Background pres for political speech maggie & gemma.ppt A 3323392 Fri Oct 24 14:31:26 2008
extra listening exercise on la télévision.doc A 24064 Wed Jan 11 08:12:32 2006
gender of nouns.ppt A 42496 Fri Sep 10 07:55:42 2004
...
63999 blocks of size 8388608. 36710 blocks available
shell_exec
的示例输出 - 注意télévision会发生什么
,vidéo
等:
Example output from shell_exec
- note what happens to télévision
, vidéo
etc.:
. D 0 Tue Oct 25 16:45:26 2011
.. D 0 Tue Oct 25 16:45:26 2011
...
Background pres for political speech maggie & gemma.ppt A 3323392 Fri Oct 24 14:31:26 2008
extra listening exercise on la t gender of nouns.ppt A 42496 Fri Sep 10 07:55:42 2004
...
63999 blocks of size 8388608. 36710 blocks available
推荐答案
为我工作的解决方案是在shell_exec之前运行这些命令,以确保使用正确的语言环境:
The solution that worked for me was to run these commands before shell_exec, to make sure that the correct locale was being used:
$locale = 'en_GB.utf-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);
假设你可以将en_GB改成你的语言。我注意到区域设置字符串似乎区分大小写。
Presumably you can just change en_GB to whatever your language is. I noticed that the locale string seems to be case sensitive.
这篇关于shell_exec()的输出包含重音的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!