我有一个简单的PHP程序,它执行一个输出日语单词的jar:
<?php
exec("java -jar PixivClipboardPostGet.jar", $output);
$text = $output[0];
echo $text;
?>
这是java文件:
public class PixivClipboardPostGet {
public static void main(String[] args) {
System.out.println("日本語");
}
}
如果我在cmd中执行它,它会打印正确的日语单词,带有chcp 932:
日本語
但当我通过PHPExec执行它时,输出是:
“ú–{Œê
我已经尝试了许多方法来更改PHP中的编码,以便它可以正确地输出文本:
尝试1:
header('content-type: text/html; charset=utf-8');
输出:
���{��
尝试2:
$locale='ja-JP.utf-8';
setlocale(LC_ALL,$locale);
putenv('LC_ALL='.$locale);
输出:
���{��
我尝试过exec('chcp'(,只是为了知道exec使用的代码页,它正确地输出了932,这是我为cmd设置的编码。
有人能启发我解决这个问题吗?提前感谢!
我在中文中遇到了C#的问题,通过对参数和响应进行base64编码来解决。
PHP
// UTF-8 without BOM.
$arg = base64_encode('日本語');
$response = exec("c:'hello.exe {$arg}");
$parse = base64_decode(urldecode($response));
C#
// Decode the argument to UTF-8
byte[] arg = Convert.FromBase64String(input);
string foo = Encoding.UTF8.GetString(farg);
...
// Output UTF-8 base64 string
byte[] string = System.Text.Encoding.UTF8.GetBytes(result);
Console.WriteLine(HttpUtility.UrlEncode(Convert.ToBase64String(string)));