【问题标题】:Strange Linux file writing奇怪的Linux文件写入
【发布时间】:2017-05-13 06:57:04
【问题描述】:

我正在使用 PHP 进行开发。我只有以下陈述:

<?php
$text = "http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/";
$text = urldecode($text);
$log_file = fopen("log.txt", "w");
$text = substr($text, -100000);
@$text = mb_convert_encoding($text, 'UTF-8', 'OLD-ENCODING');
fwrite($log_file, $text);
fclose($log_file);
?>

Windows操作系统下,日志文件内容如预期:

http://www.aecinema.ir/movie/این-پیکان-2/

但在 Linux (CentOS) 下的输出是:

http://www.aecinema.ir/movie/این-پیکان-2/

这是解码错误。

编辑:脚本由Cron 运行。我不知道这是否有什么不同。

编辑:我注意到只有当我使用浏览器打开文件时,字符才会显示得很奇怪。当我下载文件并在 Windows 中打开它时,一切正常。

【问题讨论】:

    标签: php linux file utf-8


    【解决方案1】:

    试试这个,希望对你有帮助。您应该启动一个将字符集定义为utf-8 的标头以获得所需的输出。像这样header('Content-Type: text/html; charset=utf-8');

    Try this code snippet here

    <?php
    
    ini_set('display_errors', 1);
    header('Content-Type: text/html; charset=utf-8');
    $s='http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/';
    echo $s = urldecode($s);
    

    输出: http://www.aecinema.ir/movie/این-پیکان-2/

    解决方案 2: Not a good solution than first one

    Try this code snippet here

    <?php
    
    ini_set('display_errors', 1);
    echo '<meta charset="UTF-8">';
    $s='http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/';
    echo $s = urldecode($s);
    

    完整代码:

    <?php
    ini_set('display_errors', 1);
    
    header("Content-type: text/html; charset=utf-8");
    $text = "http://www.aecinema.ir/movie/%d8%a7%db%8c%d9%86-%d9%be%db%8c%da%a9%d8%a7%d9%86-2/";
    $text = urldecode($text);
    $log_file = fopen("log.txt", "w");
    $text = substr($text, -100000);
    fwrite($log_file, $text);
    fclose($log_file);
    

    【讨论】:

    • 试过了。同样的问题,没有错误。请查看我的编辑。
    • @Mowji 你能分享你目前正在使用的代码吗?
    • 对不起。这是文件的 Linux 问题。
    • @Mowji,我不这么认为,可能是你的 linux 有问题。
    猜你喜欢
    • 2013-11-27
    • 2016-12-15
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多