我使用下面的函數來獲取HTML字符串的內部HTML的實際Unicode字符使用上一層> saveHTML()方法
function DOMinnerHTML($element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$tmp_dom = new DOMDocument('1.0', 'UTF-8');
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML .= trim($tmp_dom->saveHTML());
}
return $innerHTML;
}
我的HTML字符串還包含Unicode字符自動轉換爲數字值。這裏是當我使用上述功能
$output = DOMinnerHTML($html);
輸出是如下
$output = '<div>Thats True. Yes it is well defined
کے۔سلطا</div>';
轉換爲數字值的實際Unicode字符例如HTML字符串
$html = '<div>Thats True. Yes it is well defined آپ مجھے تم کہہ کر پکاریں</div>';
的。
我已經調試代碼,發現DOMinnerHTML函數以下行之前
$innerHTML .= trim($tmp_dom->saveHTML());
如果我贊同
echo $tmp_dom->textContent;
它顯示了實際的Unicode字符,但保存到$innerHTML
後輸出數字符號。 爲什麼這樣做。
請注意:請不要將html_entity_decode建議爲將數字符號轉換爲真正的unicode字符,因爲我也有用戶格式化的數據在我的html字符串中,我不想轉換。
注:我也通過把
<meta http-equiv="content-type" content="text/html; charset=utf-8">
我的HTML字符串,但沒有什麼區別受審。
相關:http://stackoverflow.com/questions/6573258/domdocument-and-special-characters – 2013-04-05 17:04:11
呃,是有問題嗎?數字字符引用應該仍然可以正常工作。好的,他們只需要佔用幾個字節... – bobince 2013-04-07 20:54:17