I'm importing some arbitrary HTML into a DOMDocument using the loadHTML() function, eg.:

我正在使用loadHTML()函数将一些任意HTML导入DOMDocument,例如:

$html = '<p><a href="test.php">Test</a></p>';
$doc = new DOMDocument;
$doc->loadHTML($html);

I then want to change a few attributes/node values using DOMDocument methods which I can do no problem.

然后我想使用DOMDocument方法更改一些属性/节点值,我可以毫无问题。

Once I've made these changes I'd like to export the HTML string (using ->saveHTML()), without the <html><body>... tags that the DOMDocument automatically adds to the HTML.

一旦我做了这些更改,我想导出HTML字符串(使用 - > saveHTML()),而不使用DOMDocument自动添加到HTML的 ...标记。

I understand why these are added (to ensure a valid document), but how would I go about just getting my edited HTML back (essentially everything between the <body> tags)?

我理解为什么会添加这些(以确保有效的文档),但是我如何才能将我编辑的HTML(尤其是标签之间的所有内容)放回去?

I have read this post and while it offers some solutions I would rather do this 'properly', i.e. without using a string replace on the <body> tags. Validity of the HTML is not an issue as it's run through an HTML purifier before hand.

我已经阅读了这篇文章,虽然它提供了一些解决方案,但我宁愿这样做“正确”,即不使用标签上的字符串替换。 HTML的有效性不是问题,因为它预先通过HTML净化器运行。

Any ideas? Thanks.

有任何想法吗?谢谢。

EDIT

编辑

I'm aware of the $node parameter added to saveHTML() in PHP 5.3.6, unfortunately I'm stuck with 5.2.

我知道在PHP 5.3.6中为saveHTML()添加了$ node参数,不幸的是我坚持使用5.2。

3 个解决方案

#1


4

Perhaps the source code of this will help - They're using a regex to strip out the unnecessary strings:

也许这个源代码会有所帮助 - 他们使用正则表达式删除不必要的字符串:

http://beerpla.net/projects/smartdomdocument-a-smarter-php-domdocument-class/

http://beerpla.net/projects/smartdomdocument-a-smarter-php-domdocument-class/

$content = preg_replace(array("/^\<\!DOCTYPE.*?<html><body>/si",
                                  "!</body></html>$!si"),
                            "",
                            $this->saveHTML());

return $content;

saveHTMLExact() - DOMDocument has an extremely badly designed "feature" where if the HTML code you are loading does not contain <html> and <body> tags, it adds them automatically (yup, there are no flags to turn this behavior off).

saveHTMLExact() - DOMDocument有一个设计极其糟糕的“功能”,如果你加载的HTML代码不包含和标签,它会自动添加它们(是的,没有标志可以关闭此行为) 。

Thus, when you call $doc->saveHTML(), your newly saved content now has <html><body> and DOCTYPE in it. Not very handy when trying to work with code fragments (XML has a similar problem).

因此,当您调用$ doc-> saveHTML()时,您新保存的内容现在包含 和DOCTYPE。在尝试使用代码片段时不是很方便(XML有类似的问题)。

SmartDOMDocument contains a new function called saveHTMLExact() which does exactly what you would want – it saves HTML without adding that extra garbage that DOMDocument does.

SmartDOMDocument包含一个名为saveHTMLExact()的新函数,它可以完全满足您的需要 - 它可以保存HTML而不会添加DOMDocument所做的额外垃圾。

Also, other questions have asked similar things:

此外,其他问题也提出了类似的问题:

How to saveHTML of DOMDocument without HTML wrapper?

如何在没有HTML包装的情况下保存DOMDocument的HTML?

更多相关文章

  1. PHP实现字符串转换成查询语句
  2. mysql 判断null 和 空字符串
  3. mysql如何判断一个字符串是否包含另外一个字符串?
  4. mySQL:按给定的ID字符串排序ID? (为分层数据构建面包屑)
  5. 用PHP比较PHP中的字符串
  6. 如何用C++向MYSQL插入UNICODE字符串?
  7. 由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对
  8. 如何从标准化表中获取最流行的标签?
  9. 连接相关标签的最佳数据库设计

随机推荐

  1. Android SQLite数据库操作实例
  2. android xml解析之SAX
  3. 老罗Android开发视频教程 (android常用UI
  4. SlidingDrawer 使用
  5. Android版本更新代码实现
  6. Android移动应用开发_基础页面布局总结
  7. Android Battery 分析
  8. 轻松学Android开发
  9. Android(安卓)7.0 启动篇 — init原理(一)(
  10. Error generating final archive: Debug