php exec()在unicode模式下? [英] php exec() in unicode mode?

查看:71
本文介绍了php exec()在unicode模式下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行接受ut8作为输入或生成ut8输出的命令行命令和工具. 因此,我使用cmd可以正常工作,但是当我使用exec从php尝试运行此命令时,它将无法工作. 为简单起见,我尝试了简单的输出重定向.

I need to execute command line commands and tools that accept ut8 as input or generate an ut8 output. So i use cmd an it works, but when i try this from php with exec it doesn't work. To make it simple i tried simple output redirection.

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

When i write direct in command prompt:

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

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

已创建uft8.txt,内容正确.

The uft8.txt is created an the content is correct.

цчшщюя-öüäß

цчшщюя-öüäß

当我使用php的exec函数时:

When i use the exec function from php:

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

utf8.txt中的内容被弄乱了:

the content in the utf8.txt is messed up:

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

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

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

I am using Win7,64bit with (Console) Codepage 850.

该如何解决?

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

Additional Infos: I am trying to overcome some issues with reading and writing of utf8 filenames on windows. PHP file functions fail: glob, scandir, file_exists can't handle utf8 filenames correctly. File are invisible, skipped, names are changed ... Therefore i want to avoid php file functions and i am looking for some php extern filehandling.

推荐答案

由于我找不到简单,快速和可靠的内部php解决方案,因此我以使用它结束而告终. Cmd批处理文件. 我做了一个小功能,可以在运行时生成一个cmd批处理文件. 它只是在chcp(更改代码页)命令之前添加了命令,以便切换到unicode. 并解析输出.

Since i couldn't find an easy, fast and reliable internal php solution, i am ending with using that i know it's work. Cmd-Batch-File. I make a small function that generate a cmd batch file in runtime. Its just prepends the the chcp (change the codepage) command in order to switch to unicode. And parse the output.

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():

Usage: just like php exec():

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

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

OR

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

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

这篇关于php exec()在unicode模式下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆