2011-05-26 34 views
2
我無法從這個YouTube視頻解碼標題實體

不能在標題解碼HTML實體

http://www.youtube.com/watch?v=p7NMsywVQhY

這裏是我的代碼:

$url = 'http://www.youtube.com/watch?v=p7NMsywVQhY'; 
$html = @file_get_contents($url); 
$doc = new DOMDocument(); 
@$doc->loadHTML($html); 

$nodes = $doc->getElementsByTagName('title'); 
$title = $nodes->item(0)->nodeValue; 

//decode the '‪' in the title 
$title = html_entity_decode($title,ENT_QUOTES,'UTF-8'); //does not seem to have any effect 
//decode the utf data 
$title = utf8_decode($title); 

$標題返回一切很好,除了返回問號‪最初是在標題中。

謝謝。

+2

Êa;是unicode中的「從左到右嵌入」,它不應該是可打印的字符。 – 2011-05-26 21:56:40

+0

好的,那麼我怎樣才能從字符串中刪除這些類型的代碼? – Alex 2011-05-26 22:06:10

+0

搜索和替換可能是最好的選擇。 – 2011-05-26 22:06:38

回答

0

試試這個強制正確檢測字符集:

$doc = new DOMDocument(); 
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html); 

$nodes = $doc->getElementsByTagName('title'); 
$title = $nodes->item(0)->nodeValue; 

echo $title; 
+0

如果文件不是UTF-8,這會搞砸編碼? – Alex 2011-05-26 22:04:49

+0

這實際上工作得很好。謝謝。 – Alex 2011-05-26 22:24:17

1

我不知道PHP提供的任何功能,要做到這一點,但是你可以使用preg_replace這樣的:

$string = preg_replace('/&#x([0-9a-f]+);/ei', 'chr(hexdec("$1"))', $string); 
+0

這似乎沒有做任何事情 – Alex 2011-05-26 22:05:11

+0

有了這段代碼,我得到了「*」爲&#x202a;不是嗎? – MatTheCat 2011-05-26 22:06:56

+0

嗯,當我申請這個正則表達式的問號仍然在字符串 – Alex 2011-05-26 22:08:24