doubu5154 2014-11-02 16:26
浏览 42
已采纳

Unix SSH服务器上的Cron Job:UTF-8编码?

Running in PHP 5, I have tried the following two commands to upload from a ISO-8859-1 string to a UTF-8 database that links to UTF-8 web pages:

$data[] = utf8_encode((string)$child);

... or:

$data[] = convertToUtf8((string)$child);

function convertToUtf8($text) {
//  cURL('http://'.$url);

    preg_match('/<meta.*?charset=(|\")(.*?)("|\")/i', $html, $matches);

    $charset = $matches[2];

    if($charset)
        return mb_convert_encoding($text, 'UTF-8', $charset);
    else
        return $text;
}   

If I execute the PHP script directly by accessing the file from the browser, either one of these works like a charm : the « é » imported is its nice French self. HOWEVER, when I try to execute the same script from a Unix terminal SSH (cron job OR command line), it does not convert the code and so the « é » shows up as « Ã© ».

Any ideas?? I've tried deleting the cron job and replacing the script to make sure the code was going through. This is getting maddening!

Update - the solution

It turns out this particular functionality needs a later version of PHP. Executing it in the Terminal with the following command (and writing the command as such in the cron job) takes care of the problem:

php5.4 xmluploader.php
  • 写回答

1条回答 默认 最新

  • doolo00026 2014-11-29 13:19
    关注

    Well, along the way I certainly got to explore some encoding insights but in the end, the solution was quite embarrassingly simple.

    I was running the job by typing in

    php myjob.php
    

    That was a non-starter, because the version by default on my server was too primitive and did not contain the functions utf8_encode or mb_convert_encoding. Therefore, I now start the job with the following command:

    php5.4 myjob.php
    

    ... and it works like a charm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器