gpt4 book ai didi

php exec()在unicode模式下?

转载 作者:可可西里 更新时间:2023-10-31 23:11:50 28 4
gpt4 key购买 nike

我需要执行接受 ut8 作为输入或生成 ut8 输出的命令行命令和工具。所以我使用 cmd 它可以工作,但是当我用 exec 从 php 尝试这个时它不起作用。为了简单起见,我尝试了简单的输出重定向。

当我在命令提示符下直接写入时:

chcp 65001 > nul && echo цчшщюя-öüäß>utf8.txt

uft8.txt 已创建且内容正确。

цчшщюя-öüäß

当我使用 php 的 exec 函数时:

$cmd = "chcp 65001 > nul && echo цчшщюя-öüäß>utf8.txt";
exec($cmd,$output,$return);
var_dump($cmd,$output,$return);

utf8.txt里面的内容乱七八糟:

¥Å¥Î¥^¥%¥Z¥?-ÇôǬÇÏÇY

我正在使用带有(控制台)代码页 850 的 Win7,64 位。

我应该怎么做才能解决这个问题?

附加信息:我试图克服在 Windows 上读写 utf8 文件名的一些问题。PHP 文件函数失败:glob、scandir、file_exists 无法正确处理 utf8 文件名。文件不可见,被跳过,名称被更改......因此我想避免 php 文件函数,我正在寻找一些 php extern 文件处理。

最佳答案

因为我找不到简单、快速和可靠的内部 php 解决方案,所以我将结束使用我知道它有效的解决方案。 Cmd-批处理文件。我制作了一个在运行时生成 cmd 批处理文件的小函数。它只是在 chcp(更改代码页)命令前面添加以切换到 unicode。并解析输出。

function uft8_exec($cmd,&$output=null,&$return=null)
{
//get current work directory
$cd = getcwd();

// on multilines commands the line should be ended with "\r\n"
// otherwise if unicode text is there, parsing errors may occur
$cmd = "@echo off
@chcp 65001 > nul
@cd \"$cd\"
".$cmd;


//create a temporary cmd-batch-file
//need to be extended with unique generic tempnames
$tempfile = 'php_exec.bat';
file_put_contents($tempfile,$cmd);

//execute the batch
exec("start /b ".$tempfile,$output,$return);

// get rid of the last two lin of the output: an empty and a prompt
array_pop($output);
array_pop($output);

//if only one line output, return only the extracted value
if(count($output) == 1)
{
$output = $output[0];
}

//delete the batch-tempfile
unlink($tempfile);

return $output;

}

用法:就像 php exec() 一样:

utf8_exec('echo цчшщюя-öüäß>utf8.txt');

uft8_exec('echo цчшщюя-öüäß',$output,$return);

关于php exec()在unicode模式下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13332321/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com