如果我使用echo $doc->saveHTML();
它會相應地顯示字符,但是一旦它到達xml?在xpath中提取元素,問題又回來了。xpath後的PHP DomXPath編碼問題
我似乎無法正確顯示字符。我如何正確轉換它。我越來越:
婢跺繐顒滈拺鍙ョ瀵偓鐞涱偊鈧繑妲戦挅鍕綍婢舵牕顨� 闂€鍌溾敄緙侊綀濮蟲稉濠呫€� 娑擃叀顣葷純鎴犵綍閺冭泛鐨繪總鍏呯瑐鐞涳綀鏉藉▎
而不是正確的中國:
<head><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta charset="gbk"/></head>
我的PHP代碼:
$html = file_get_contents('http://item.taobao.com/item.htm?spm=a2106.m874.1000384.41.aG3Kbi&id=20811635147&_u=o1ffj7oi9ad3&scm=1029.newlist-0.1.16&ppath=&sku=');
$doc = new DOMDocument();
// Based on Article http://stackoverflow.com/questions/11309194/php-domdocument-failing-to-handle-utf-8-characters/11310258#11310258
$searchPage = mb_convert_encoding($html,"HTML-ENTITIES","GBK");
$doc->loadHTML($searchPage);
// echo $doc->saveHTML();
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*[@id='detail']/div[1]/h3");
foreach ($elements as $e) {
//echo $e->nodeValue;
echo mb_convert_encoding($e->nodeValue,"utf-8","gbk");
}
'gbk'編碼[不支持](http://php.net/manual/en/mbstring.supported-encodings.php)。改爲使用「GB18030」或「EUC-CN」。 –
@HashemQolami但我設法mv_convert沒有問題的GBK,它只是當它成爲一個DOMpath($ doc)的東西出錯了? – CodeGuru