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.

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

报告相同问题?