views:

51

answers:

2

Hi! 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.

A: 

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...

Mike
Thanks, but when i try print the content, dont display correctly.
turbod
A: 

Initialize DOM like this:

$dom->loadHTML('<?xml encoding="UTF-8">' . $data);
Richard Knop
unfortunally not solved the problem.
turbod