Mar*_*dez 2 php browser encoding utf-8
我有这个 php 脚本:
\n\n<?php\n $cmd_desformat = "/usr/local/bin/process /tmp/input.txt > /tmp/output.txt";\n shell_exec($cmd_desformat);\nRun Code Online (Sandbox Code Playgroud)\n\n其中 input.txt 是一个 UTF-8 文件(使用“file -i”检查)包含:
\n\nBuenos d\xc3\xadas\nRun Code Online (Sandbox Code Playgroud)\n\n/usr/local/bin/process 是来自第三方的二进制可执行文件,我广泛使用过它,但从未遇到过这个问题。
\n\n好吧,问题是当我从浏览器执行此操作时:
\n\nhttp://localhost/test.php\nRun Code Online (Sandbox Code Playgroud)\n\n结果output.txt是一个US-ASCII文件,包含:
\n\nBuenos d?as [][\nRun Code Online (Sandbox Code Playgroud)\n\n但是,当我从命令行执行此操作时:
\n\nphp test.php\nRun Code Online (Sandbox Code Playgroud)\n\n结果output.txt是一个UTF-8文件,其内容如下:
\n\nBuenos d\xc3\xadas [][\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试与用户一起从命令行执行,www-data看看是否可以复制浏览器行为,但结果又是正确的。我也尝试过使用exec而不是shell_exec但结果是一样的。还尝试过 Firefox 和 Chrome。
我需要它在从浏览器调用时工作。有任何想法吗?
\nPHP CLI 环境与此不一样shell_exec。如何设法让您的内容以正确的方式返回有两种可能性。
env -i最简单的方法就是通过调用来重置环境shell_exec。
<?php
$cmd_desformat = "env -i /usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
shell_exec($cmd_desformat);
Run Code Online (Sandbox Code Playgroud)
如果默认环境设置不正确,这可能不起作用。如果是这种情况,那么您可能需要使用 显式设置它putenv()。
<?php
putenv('LANG=en_US.UTF-8'); // Set this to the language you need
$cmd_desformat = "/usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
shell_exec($cmd_desformat);
Run Code Online (Sandbox Code Playgroud)