开发者

Character encoding error!

I try get the content this URL: http://www.chromeball.com, but the character encoding is not good.

I have this code:

$url = 'http://www.chromeball.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec(开发者_运维知识库$ch);
curl_close($ch);


$dom = new DOMDocument();
$dom->loadHTML($data);
$xpath = new DOMXPath($dom);  
$nodes = $xpath->query('//text() | //@alt | //@title | /html/head/meta[@name="description"] | /html/head/meta[@name="keywords"]');
foreach($nodes as $node) {
  $textNodeContent .= " ".$node->nodeValue;
}

$enc = mb_detect_encoding($textNodeContent,'iso-8859-2,iso-8859-1,utf-8');
print  iconv($enc,'utf-8//TRANSLIT',$textNodeContent);

But this not working. The character encoding is wrong. How can i convert the $textNodeContent to utf-8? Thanks.


Initialize DOM like this:

$dom->loadHTML('<?xml encoding="UTF-8">' . $data);


From the comments on the mb_detect_encoding page, it looks as though the function is not particularly reliable. Chrigu (see post dated 29-Mar-2005 03:32), suggests placing UTF-8 as the first character encoding in the list:

$enc = mb_detect_encoding($textNodeContent,'utf-8,iso-8859-2,iso-8859-1');

I've tried it, and it now shows the content as being UTF-8. However, I've just tried it with ISO-8859-1 content, and it detects that as UTF-8 too...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜