【问题标题】:file_get_contents encoding - working Chrome and Safari, not working Firefox, Opera, IEfile_get_contents 编码 - 工作 Chrome 和 Safari,不工作 Firefox、Opera、IE
【发布时间】:2013-03-09 07:12:03
【问题描述】:

从几天开始,我试图实现一些代码来将一些示例内容从另一个站点加载到我的站点。我有编码问题 - 波兰语。 源站点是 ISO-8859-2,目标是 UTF-8。 它适用于 Chrome 和 Safari,不适用于 FF、Opera 和 IE。我做错了什么?

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test_site</title>



<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
<script type="text/javascript">
    $("document").ready(function() {

        $("#content").load("curl.php #news_ajax");

    });
</script>


</head>
<body>

<h1>Test site</h1>
<div id="content"><img src="ajax-loader.gif" alt="Loading..." /></div>

</body>
</html>

curl.php

<?php
    $url = 'http://www.dominikanie.pl/';
    $htm = file_get_contents($url);
    $domain = "http://www.dominikanie.pl/";
    $htm = preg_replace("/(href|src)\=\"([^(http)])(\/)?/", "$1=\"$domain$2", $htm);
    $htm = mb_convert_encoding($htm, "ISO-8859-2",
          mb_detect_encoding($htm, "UTF-8, ISO-8859-2", true));
    echo $htm;

?>

我尝试了 iconv 但没有结果。测试site

【问题讨论】:

    标签: php jquery cross-browser file-get-contents


    【解决方案1】:

    源站点为 ISO-8859-2,目标为 UTF-8

    应该是这样的

    $htm = mb_convert_encoding($htm, "UTF-8",
          mb_detect_encoding($htm, "UTF-8, ISO-8859-2", true));
    

    【讨论】:

      【解决方案2】:
      • Web 浏览器与 file_get_contents 无关。

      • 使用 CURL 代替 file_get_content。文档here

      • 此外,dominikanie.pl(源代码)是 UTF-8,而不是 ISO。这就是您的编码不起作用的原因。

      • 通过 AJAX 查询数据时,可以尝试将数据作为 XML 或 json 对象发送。

      • 使用较新的 jQuery

      • iconv vs mb - 我更喜欢 iconv。另外我的经验是编码检测并不总是能正常工作。特别是当没有太多数据要测试或者有一些奇怪的实体比如 MsWord 特殊字符(比如波兰语“”)时

      • str_repleace 有时会出现波兰字符问题。它很少见,但我过去曾遇到过一些问题。也不要使用 htmlentities()。它真的很喜欢破坏 PL 字符:]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-27
        • 2012-08-04
        相关资源
        最近更新 更多