我正在使用以下函数来获取 html 字符串的内部 html
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 字符。这是html字符串的示例
$html = '<div>Thats True. Yes it is well defined آپ مجھے تم کہہ کر پکاریں</div>';
当我使用上述功能时
$output = DOMinnerHTML($html);
输出如下
$output = '<div>Thats True. Yes it is well defined
کے۔سلطا</div>';
转换为数值的实际 unicode 字符。
我已经调试了代码,发现在下面一行之前的 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 字符串之前,但没有区别。