2013-03-20 127 views
1

从几天我试图实现一些代码来从另一个网站加载一些示例内容到我的网站。我有编码问题 - 波兰语。 源站点是ISO-8859-2,目标是UTF-8。 它在Chrome和Safari中工作,不在FF,Opera和IE中工作。我究竟做错了什么?file_get_contents编码 - 工作的Chrome和Safari,不工作Firefox,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

回答

2
  • Web浏览器与file_get_contents无关。

  • 使用CURL而不是file_get_content。文档here

  • 另外dominikanie.pl(来源)是在UTF-8,而不是ISO。这就是为什么你的编码不​​起作用。

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

  • 使用新的jQuery

  • iconv vs mb - 我喜欢的iconv。另外我的经验是,编码检测并不总是像它应该的那样工作。特别是当没有太多的数据要测试或者有一些奇怪的实体像MsWord特殊字符(如Polish“”)

  • str_repleace有时会遇到波兰字符问题。这很少见,但我过去曾遇到过一些问题。也不要使用htmlentities()。它真的想分手PL字符:]

1

源基地通过了ISO-8859-2和目标UTF-8

所以应该

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