如何保持< br />标签在php中使用Dom来解析html文档? [英] how to keep <br/> tags when using Dom in php to parse html document?
本文介绍了如何保持< br />标签在php中使用Dom来解析html文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在php中使用dom通过 $ node-> nodeValue
来检索div的内容。该div在其内容中有许多< br />
标签,但是在我将其存储在数据库中并在浏览器中输出之后,所有的< br />
标签更改为空格。我想保留< br />
标签,我如何实现?
i use dom in php to retrieve a div's content by $node->nodeValue
. This div has many <br/>
tags in its content, but after i store it in the database and output it in the browser, all the <br/>
tags are changed to the whitespace. I want to keep the <br/>
tags, how do i achieve that?
推荐答案
DOMNode :: nodeValue
只会返回文本内容。
As < br />
是一个子元素,它不会被返回。
As <br />
is a child element, it won't be returned.
你最好的选择是<
Your best bet is to
- 创建一个空的临时字符串
- 循环
$ node
- 使用
获取每个子节点的标记DOMDocument :: saveHTML()
- 将此字符串与您的temp连接
- 将临时字符串保存到数据库
- Create an empty, temporary string
- Loop over all the child nodes in your
$node
- Get the markup of each child node using
DOMDocument::saveHTML()
- Concatenate this string with your temp one
- Save the temp string to the database
这样的一个 - http:// www。 php.net/manual/en/book.dom.php#89718
这篇关于如何保持< br />标签在php中使用Dom来解析html文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文