【问题标题】:How to convert output of windows shell exec in php to utf8如何将php中windows shell exec的输出转换为utf8
【发布时间】:2017-12-22 13:14:51
【问题描述】:

我正在尝试将 PHP 中执行 shell 的命令之一的输出转换为 utf8,因为 json_encode 返回一个空字符串。

我尝试使用mb_detect_encoding 来检测编码,我想使用mb_convert_encoding,但mb_detect_encoding 返回的是空字符串。

我有这样的输出:

2016-04-29  10:09         2�216�720 0.8.1.zip

我也试过这个:

sapi_windows_cp_conv(sapi_windows_cp_get(), 65001, $output);

但是这个返回:

sapi_windows_cp_conv(): Wide char conversion failed

在这个问题PHP UTF-8 to Windows command line encoding中找到的输出代码页

在执行 dir 之前还尝试转换为 utf8:

shell_exec('chcp 65001 && dir');

但我得到了相同的结果,很奇怪——而不是空格和空 mb_detect。

如何将windows上shell命令的输出转换为UTF8?

【问题讨论】:

  • shell 使用什么编码?也许echo $LANG 有编码信息。
  • @Mat 是 windows cmd.exe 不是 unix。
  • 我们能否得到一些产生该输出的输入?另见what-encoding-code-page-is-cmd-exe-using
  • @Tzalumen 这是dir 的输出,chcp 大约是 47x,我现在无法访问那台 Windows 机器。

标签: php windows shell utf-8 cmd


【解决方案1】:

函数sapi_windows_cp_get()接受字符串作为参数

$output = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), 65001, $output);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 2011-01-03
    • 2013-04-09
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多