php - DOMDocument->saveHTML() 转换为空格

标签 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>

如何防止   被转换为空格?

最佳答案

$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);

关于php - DOMDocument->saveHTML() 转换为空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114627/

相关文章:

php - 使用 htmlentities 获取 €

php - 计算不同表的平均值

php - 如何在一个查询中从三个表中进行选择

php - 是否可以让 wordpress 用户知道他们已经阅读了哪些帖子?

php - 使用 DOMXpath::query 获取 id 旁边的第一张图片

unicode - 如何使用 Unicode 进行 SEO?

php - 如何使用 PHP 和我的 sql 在循环中动态设置图像标签内的值

PHP - 删除 &lt;script&gt; 中的所有内容和 HTML 字符串的 CDATA

java - fatal error - 序言中不允许内容

php - 编码 HTML 实体但忽略 HTML 标签 - 在 PHP 中