David
David

Reputation: 2721

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

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?

Upvotes: 5

Views: 4794

Answers (3)

Phil
Phil

Reputation: 164731

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

Upvotes: 2

Dr.Molle
Dr.Molle

Reputation: 117314

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

$node->ownerDocument->saveXML($node);

Upvotes: 9

ubiquibacon
ubiquibacon

Reputation: 10667

Assuming you are using MySQL (since you don't say) make sure you use the function mysql_real_escape_string. Dr.Molle's answer might provide further insight.

http://php.net/manual/en/function.mysql-real-escape-string.php

Upvotes: -1

Related Questions