命令行字符编码从PHP的exec() [英] Command line character encoding from PHP's exec()
问题描述
我试图传递UTF-8文本作为参数到命令行程序使用php的 exec
函数,并有问题。我怀疑这是由于字符编码问题。
I'm trying to pass UTF-8 text as an argument to a command line program using php's exec
function and have been having problems. I suspect it's due to character encoding issues.
当我从命令行调用 locale charmap
UTF-8
。
但是当我这样做:
<?php echo exec('locale charmap'); ?>
我得到
ANSI_X3.4-1968
如何更改?这不是我想要设置的一次 - 我将使用不同的语言/编码潜在地,所以每次调用 exec()
可能需要一个不同的。但是,即使知道如何将它设置为 UTF-8
一次也是有帮助的。
How can I change this? It's not something I want to set once -- I'll be working with different languages / encodings potentially so each call to exec()
could require a different one. But even knowing how to set it to UTF-8
once would be helpful.
推荐答案
使用 LANG =< language> .UTF8
前缀命令。
示例:
$ php -r "echo exec('LANG=\"en_US.UTF8\" locale charmap');"
UTF-8
$ php -r "echo exec('LANG=\"en_US.iso88591\" locale charmap');
ISO-8859-1
您应该安装了语言环境。
You should have the locale installed.
这篇关于命令行字符编码从PHP的exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!