如何保持< br />标签在php中使用Dom来解析html文档? [英] how to keep <br/> tags when using Dom in php to parse html document?

查看:98
本文介绍了如何保持< 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


  1. 创建一个空的临时字符串

  2. 循环 $ node

  3. 使用获取每个子节点的标记DOMDocument :: saveHTML()

  4. 将此字符串与您的temp连接

  5. 将临时字符串保存到数据库

  1. Create an empty, temporary string
  2. Loop over all the child nodes in your $node
  3. Get the markup of each child node using DOMDocument::saveHTML()
  4. Concatenate this string with your temp one
  5. Save the temp string to the database

这样的一个 - http:// www。 php.net/manual/en/book.dom.php#89718

这篇关于如何保持&lt; br /&gt;标签在php中使用Dom来解析html文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆