Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep <br/> tags when using Dom in php to parse html document?

Tags:

dom

php

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?

like image 970
David Avatar asked May 03 '11 02:05

David


People also ask

What is loadHTML?

DOMDocument::loadHTMLThe function parses the HTML contained in the string source . Unlike loading XML, HTML does not have to be well-formed to load. This function may also be called statically to load and create a DOMDocument object.


2 Answers

nodeValue returns only the text-data (if used on element-nodes). Retrieve the contents using saveXML()

$node->ownerDocument->saveXML($node);
like image 89
Dr.Molle Avatar answered Nov 14 '22 22:11

Dr.Molle


DOMNode::nodeValue will only return the text content.

As <br /> is a child element, it won't be returned.

Your best bet is to

  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

Something like this - http://www.php.net/manual/en/book.dom.php#89718

like image 41
Phil Avatar answered Nov 14 '22 23:11

Phil