PHP exec更改编码 [英] PHP exec change encoding

查看:104
本文介绍了PHP exec更改编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用php exec命令来解决UTF-8文件名。问题是php exec 命令似乎不了解utf-8。我使用这样的东西:

I need to address UTF-8 filenames with the php exec command. The problem is that the php exec command does not seem to understand utf-8. I use something like this:

echo exec('locale charmap');

返回 ANSI_X3.4-1968

查看这个 SO问题,解决方案如下所示:

looking at this SO question, the solution lookes like that:

echo exec('LANG=de_DE.utf8; locale charmap'); 

但我仍然得到相同的输出: ANSI_X3.4-1968

But I still get the same output: ANSI_X3.4-1968

另一方面 - 如果我在bash命令行上执行此php命令:

On the other hand - if I execute this php command on the bash command line:

php -r "echo exec('LANG=de_DE.UTF8 locale charmap');"

输出为UTF-8。
所以问题是:

The output is UTF-8. So the questions are:


  1. 为什么在bash和apache_module / web页面执行php命令有不同的结果?

  2. 如果将 exec 设置为UTF-8,如果它作为apache模块运行在网站内?

  1. Why is there an different result be executing the php command at bash and at apache_module/web page?
  2. How to set UTF-8 for exec if it runs inside a website as apache module?


推荐答案

为了回答我自己的问题 - 我发现了以下解决方案:

To answer my own question - i found the following solution:

使用PHP设置区域环境变量

setting the locale environment variable with PHP

$locale='de_DE.UTF-8';
setlocale(LC_ALL,$locale);
putenv('LC_ALL='.$locale);
echo exec('locale charmap');

这将设置为/返回 UTF-8 。所以我可以通过特殊字符和变音符到linux shell命令。

This sets to / returns UTF-8. So i'm able to pass special characters and umlauts to linux shell commands.

这篇关于PHP exec更改编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆