dongqiang8683 2015-07-05 23:26
浏览 186

PHP Exec和命令提示符(cmd)中的不同编码

I have this simple PHP Program where it executes a jar that outputs a japanese word:

<?php
exec("java -jar PixivClipboardPostGet.jar", $output);
$text = $output[0];
echo $text;
?>

And here's the java file:

public class PixivClipboardPostGet {
    public static void main(String[] args) {       
    System.out.println("日本語");
    }
}

If I execute it in cmd, it prints the correct Japanese words, with chcp 932:

日本語

But when I execute it via PHP Exec, here's the output:

“ú–{Œê

I've tried numerous ways to change the encoding in the PHP so it can output the text correctly:

Attempt 1:

header('content-type: text/html; charset=utf-8');

Output:

���{��

Attempt 2:

$locale='ja-JP.utf-8';
setlocale(LC_ALL,$locale);
putenv('LC_ALL='.$locale);

Output:

���{��

I've tried exec ('chcp') just to know the code page the exec used and it correctly outputs 932, the encoding I set for my cmd.

Can anybody enlighten me to solve this problem? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doufangxie0203 2016-10-28 02:11
    关注

    I had the problem with C# in chinese and resolved by base64 encoding for arguments and response.

    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)));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条