5

在我的代码中,我有

$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;转换为空格?

4

2 回答 2

4
$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);
于 2012-04-12T03:16:08.747 回答
0

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

于 2012-04-11T22:28:33.863 回答