DOMDocument-> saveHTML()转换为空格

时间:2012-04-11 22:04:06

标签: php domdocument html-entities

在我的代码中,我有

$document = DomDocument->loadHTML($someHTML);
$xPath = new DOMXPath($document);
//
//do some xpath query and processing
//
$result = $document->saveHTML();

我正在处理的html包含:

<html>
<body>
<p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal;text-autospace:none"><b><span style='font-size:9.0pt;font-family:"ArialNarrow","sans-serif";
color:red'>&nbsp;</span></b></p>
</body>
</html>

并导致:

<html>
<body>
<p class="MsoNormal" style="margin-bottom:0in;margin-bottom:.0001pt;line-height:
normal;text-autospace:none"><b><span style='font-size:9.0pt;font-family:"ArialNarrow","sans-serif";
color:red'> </span></b></p>
</body>
</html>

如何阻止&nbsp;转换为空格?

2 个答案:

答案 0 :(得分:3)

$someHTML = str_replace ('&nbsp;', '@nbsp;', $someHTML);
$document = DomDocument->loadHTML($someHTML);
$xPath = new DOMXPath($document);
//
//do some xpath query and processing
//
$result = $document->saveHTML();
$result = str_replace ('@nbsp;', '&nbsp;', $result);

答案 1 :(得分:0)

替换&amp; nbsp;与&amp; amp; nbsp; 然后当读取htmlDom文档时,它将返回&amp; nbsp;