从几天我试图实现一些代码从另一个网站加载一些示例内容到我的网站。我有编码问题-波兰语言。源站点是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,但是没有结果。测试网站
-
Web浏览器与file_get_contents无关
-
使用CURL代替file_get_content。这里的文档
-
dominikanie.pl(源代码)是UTF-8格式,而不是ISO。这就是为什么你的编码不工作
-
当你通过AJAX查询数据时,你可以尝试发送XML或jSon对象
使用更新的jQuery
iconv vs mb -我更喜欢iconv。此外,我的经验是,编码检测并不总是工作,因为它应该。特别是当没有太多的数据来测试,或者如果有一些奇怪的实体,如MsWord特殊字符(如波兰")
str_replace有时会出现波兰字符的问题。它很少见,但我以前用过它。也不要使用htmlentities()。
源站点是ISO-8859-2,目标是UTF-8
所以应该是
$htm = mb_convert_encoding($htm, "UTF-8",
mb_detect_encoding($htm, "UTF-8, ISO-8859-2", true));