简体   繁体   English

file_get_contents编码 - 使用Chrome和Safari,不使用Firefox,Opera,IE

[英]file_get_contents encoding - working Chrome and Safari, not working Firefox, Opera, IE

From few days I'm trying to implement some code to load some example content from another site to my site. 从几天开始,我正在尝试实现一些代码,将一些示例内容从另一个站点加载到我的站点。 I have problem with encoding - polish language. 我有编码问题 - 波兰语。 Source site is ISO-8859-2 and target in UTF-8. 源站点是ISO-8859-2,目标是UTF-8。 It's working in Chrome and Safari, not working in FF, Opera and IE. 它适用于Chrome和Safari,不适用于FF,Opera和IE。 What am I doing wrong? 我究竟做错了什么?

index.php 的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 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;

?>

I tried iconv but no result. 我试过iconv但没有结果。 Test site 测试现场

  • Web browser have nothing to do with file_get_contents. Web浏览器与file_get_contents无关。

  • Use CURL instead of file_get_content. 使用CURL而不是file_get_content。 Documentation here 文档在这里

  • Also dominikanie.pl (source) is in UTF-8, not ISO. dominikanie.pl(来源)也是UTF-8,而不是ISO。 This is why your encoding doesn't work. 这就是您的编码不起作用的原因。

  • You can try to send data as XML or jSon object when querying it via AJAX. 在通过AJAX查询时,您可以尝试将数据作为XML或jSon对象发送。

  • Use newer jQuery 使用更新的jQuery

  • iconv vs mb - I prefer iconv. iconv vs mb - 我更喜欢iconv。 Also my experience is that encoding detect not always work as it should. 另外我的经验是编码检测并不总是按预期工作。 Especially when there is not much data to test or if there are some weird entities like MsWord special chars (like Polish "") 特别是当没有太多的数据需要测试或者有一些奇怪的实体如MsWord特殊字符(如波兰语“”)

  • str_repleace sometimes have problems with Polish chars. str_repleace有时会出现波兰字符问题。 Its rare, but i had some problems with it in the past. 它很少见,但过去我遇到了一些问题。 Also don't use htmlentities(). 也不要使用htmlentities()。 It really like to broke PL chars :] 它真的很想打破PL字符:]

Source site is ISO-8859-2 and target in UTF-8 源站点是ISO-8859-2,目标是UTF-8

So it should be 所以它应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM