【问题标题】:file_get_contents not working with utf8file_get_contents 不适用于 utf8
【发布时间】:2011-06-22 12:12:29
【问题描述】:

我正在尝试从网站获取泰语字符。我试过了:

$rawChapter = file_get_contents("URL");
$rawChapter = mb_convert_encoding($rawChapter, 'UTF-8', mb_detect_encoding($rawChapter, 'UTF-8, ISO-8859-1', true));

当我这样做时,字符会像这样回来:

¡ÅѺ˹éÒáá¾Ã¡¤ÑÁÀÕÃìÀÒÉÒä·Â©ºÑº

但是,如果我获取我尝试加载的页面源并将其作为 utf8 文件保存到我自己的本地主机上的 .htm 文件中,那么它会正确加载泰语字符。只有当我尝试直接从网站加载它时它才会中断。

我该如何解决这个问题?可能是什么问题?

我也尝试过添加这个上下文:

$context = stream_context_create(array(
            'http' => array(
                'method' => 'POST',
                'header' => implode("\r\n", array(
                    'Content-type: application/x-www-form-urlencoded',
                    'Accept-Language: en-us,en;q=0.5',
                    'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'
                ))
            )
        ));

我尝试单独添加它,我尝试使用 mb_convert_encoding() 添加它...我觉得我已经尝试了这些东西的所有组合,但没有成功。

【问题讨论】:

    标签: php utf-8 character file-get-contents


    【解决方案1】:

    将您的 Accept-Charset 更改为 UTF-8,因为 ISO-8859-1 不支持泰语字符。如果您在 Windows 机器上运行 PHP 脚本,您也可以使用 windows-874 字符集,您也可以尝试添加此标头:

    Content-Language: th
    

    但在大多数情况下,UTF-8 将处理几乎大多数字符或字符集,而无需任何其他声明。

    ** 更新 **

    很奇怪,但这对我有用。

    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=> implode("\r\n", array(
                       'Content-type: text/plain; charset=TIS-620'
                       //'Content-type: text/plain; charset=windows-874'  // same thing
                    ))
      )
    );
    
    $context = stream_context_create($opts);
    
    //$fp = fopen('http://thaipope.org/webbible/01_002.htm', 'rb', false, $context);
    //$contents = stream_get_contents($fp);
    //fclose($fp);
    $contents = file_get_contents("http://thaipope.org/webbible/01_002.htm",false, $context);
    
    header('Content-type: text/html; charset=TIS-620');
    //header('Content-type: text/html; charset=windows-874');  // same thing
    
    echo $contents;
    

    显然,我对 UTF-8 的理解是错误的。有关详细信息,请参阅here。虽然你仍然可以有一个 UTF-8 输出:

    $in_charset = 'TIS-620';   // == 'windows-874'
    $out_charset = 'utf-8';
    
    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=> implode("\r\n", array(
                       'Content-type: text/plain; charset=' . $in_charset
                    ))
      )
    );
    
    $context = stream_context_create($opts);
    
    $contents = file_get_contents("http://thaipope.org/webbible/01_002.htm",false, $context);
    if ($in_charset != $out_charset) {
        $contents = iconv($in_charset, $out_charset, $contents);
    }
    
    header('Content-type: text/html; charset=' . $out_charset);
    
    echo $contents;   // output in UTF-8
    

    【讨论】:

    • 好的,我取出ISO并保留utf-8部分并添加该标题并将其放入:
    • 哇,我不知道如何使用 cmets... 我得到了这个:$rawChapter = file_get_contents("thaipope.org/webbible/01_002.htm",false, $context); 它返回:��Ѻ˹�� �����������������©�Ѻ
    • 是的,你的字符串没问题。您看到的问题是您的字符串很好(包含泰语字符),但您使用 ISO-8859-1 回显它。如果您的输出是 HTML,请使用 header('Content-type: text/html; charset=utf-8'); 如果您的输出是纯文本,请使用 header('Content-type: text/plain; charset=utf-8');
    • 我尝试加载另一个泰国站点,它可以正确回显,如果我复制该泰国站点的源并将其放在我的本地主机上,它也会正确回显。我会尝试添加你说的内容。
    • 我建议您始终使用 UTF-8;也就是将您的 PHP 源文件保存为 UTF-8 并始终使用 charset=utf-8。这样,您就不会遇到编码或字符乱码问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2017-12-18
    • 2013-03-20
    相关资源
    最近更新 更多