php exec()在unicode模式下?

前端之家收集整理的这篇文章主要介绍了php exec()在unicode模式下?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要执行命令行命令和工具,接受ut8作为输入或生成ut8输出.
所以我使用cmd它的工作原理,但是当我从PHP用exec尝试这个时它不起作用.
为了简单起见我尝试了简单的输出重定向.

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

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

创建了uft8.txt,内容是正确的.

цчшщюя-öüäß

当我使用PHP的exec函数时:

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

utf8.txt中的内容搞砸了:

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

我正在使用Win7,64bit和(控制台)代码页850.

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

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

由于我找不到一个简单,快速和可靠的内部PHP解决方案,我结束使用我知道它的工作. CMD-批处理文件.
我创建了一个在运行时生成cmd批处理文件的小函数.
它只是预先设置chcp(更改代码页)命令以切换到unicode.
并解析输出.
  1. function uft8_exec($cmd,&$output=null,&$return=null)
  2. {
  3. //get current work directory
  4. $cd = getcwd();
  5.  
  6. // on multilines commands the line should be ended with "\r\n"
  7. // otherwise if unicode text is there,parsing errors may occur
  8. $cmd = "@echo off
  9. @chcp 65001 > nul
  10. @cd \"$cd\"
  11. ".$cmd;
  12.  
  13.  
  14. //create a temporary cmd-batch-file
  15. //need to be extended with unique generic tempnames
  16. $tempfile = 'PHP_exec.bat';
  17. file_put_contents($tempfile,$cmd);
  18.  
  19. //execute the batch
  20. exec("start /b ".$tempfile,$return);
  21.  
  22. // get rid of the last two lin of the output: an empty and a prompt
  23. array_pop($output);
  24. array_pop($output);
  25.  
  26. //if only one line output,return only the extracted value
  27. if(count($output) == 1)
  28. {
  29. $output = $output[0];
  30. }
  31.  
  32. //delete the batch-tempfile
  33. unlink($tempfile);
  34.  
  35. return $output;
  36.  
  37. }

用法:就像PHP exec():

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

要么

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

猜你在找的PHP相关文章