PHP exec 更改编码

标签 php encoding utf-8 exec

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

echo exec('locale charmap');

返回 ANSI_X3.4-1968

看着this所以问题,解决方案看起来像这样:

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

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

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

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

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

  1. 为什么在 bash 和 apache_module/web 页面执行 php 命令的结果不同?
  2. 如果 exec 在网站内作为 apache 模块运行,如何设置 UTF-8?

最佳答案

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

用PHP设置locale环境变量

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

这设置为/返回 UTF-8。所以我能够将特殊字符和变音符号传递给 linux shell 命令。

关于PHP exec 更改编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961870/

相关文章:

php - 将旧代码升级到较新的 MySQL 版本

assembly - 在同一机器代码中编码多条指令

php - 似乎 Cron 工作在停止后仍在工作

PHP Xpath : How to get the action URL from any method on a WSDL?

c# - 如何在 C# 中将字符串转换为 UTF-8?

javascript - 如何处理 node.js 中/来自 node.js 的 (cli) 输出的标准输出流的字符编码?

java - 将编码的 UTF-8 字符转换为 android 中的特殊重音字符

javascript - jQuery HTML 追加导致 UTF-8 国家语言字符问题

c++ - UTF-16 到 UTF8 与 WideCharToMultiByte 问题

php - PHP的注意 undefined variable