unicode模式下的php exec()?
我需要执行接受ut8作为输入或生成ut8输出的命令行命令和工具。 所以我使用cmd,它可以工作,但是当我从php和exec尝试这个时,它不工作。 为了简单起见,我尝试了简单的输出重定向 在命令提示符下直接写入时: chcp 65001>nul和echoчччфччфф-öääß>utf8.txt uft8.txt是在内容正确的情况下创建的 ч 当我使用php中的exec函数时:unicode模式下的php exec()?,php,utf-8,cmd,exec,Php,Utf 8,Cmd,Exec,我需要执行接受ut8作为输入或生成ut8输出的命令行命令和工具。 所以我使用cmd,它可以工作,但是当我从php和exec尝试这个时,它不工作。 为了简单起见,我尝试了简单的输出重定向 在命令提示符下直接写入时: chcp 65001>nul和echoчччфччфф-öääß>utf8.txt uft8.txt是在内容正确的情况下创建的 ч 当我使用php中的exec函数时: $cmd = "chcp 65001 > nul && echo цчшщюя-öüäß>
$cmd = "chcp 65001 > nul && echo цчшщюя-öüäß>utf8.txt";
exec($cmd,$output,$return);
var_dump($cmd,$output,$return);
utf8.txt中的内容混乱:
䵊ÎÎÎÎÎÎÎÎÎÎÎ
我正在使用Win7,64位和(控制台)代码页850
我该怎么做才能解决这个问题
其他信息:
我试图克服在windows上读写utf8文件名的一些问题。
PHP文件函数失败:glob、scandir、file\u无法正确处理utf8文件名。文件不可见,已跳过,名称已更改。。。
因此,我想避免使用php文件函数,我正在寻找一些php外部文件处理。因为我找不到一个简单、快速、可靠的内部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.net/fwrite通过管道将字符写入文件。我可以使用文件内容立即直接写入字符串,但这不是重点。我的例子只是这个问题的一个简单表示。我在问题中添加了额外的信息。嗯,php不支持utf-8或任何其他字符集,所以您可能在尝试不可能的事情。我不知道windows是如何处理控制台应用程序的字符集的,所以我不能帮上忙。看起来这对我来说可能真的有用。我需要在Windows中使用Unicode字符创建/重命名文件,到目前为止,这是唯一一个真正做到这一点的解决方案。我只需要解决写不存在的管道问题,然后我将最终能够完成这个项目。非常感谢。你帮了我一天。非常感谢你!