|
Posted by Janwillem Borleffs on 12/23/06 23:49
exhuma.twn wrote:
> I am having trouble inserting text containing newlines as text nodes.
> It replaces the newlines with proper entities ( ) and <br/>'s with
> "<br />". That's a very useful feature as such. But in my case
> it breaks what I would like to do.
>
Main problem here, is that you should rely on the content being valid XML
all the time. Especially when the content originates from user input (e.g.
through a CMS), this is hard to accomplish.
One safe solution would be to strip <br> tags and append elements/text nodes
where appropriate:
function eval_text_node_with_br(&$doc, &$child, $text) {
$text = preg_replace('!<br[^/>]*/?>!i', "\n", $text);
$text_array = preg_split('/(\r\n|\r|\n)/', $text);
foreach ($text_array as $i => $line) {
$child->append_child($doc->create_text_node($line));
$child->append_child($doc->create_element('br'));
}
}
$doc = domxml_new_doc("1.0");
$child = $doc->create_element('root');
eval_text_node_with_br($doc, $child, 'hello<br> world<BR />5');
$doc->append_child($child);
print htmlentities($doc->dump_mem());
HTH;
JW
[Back to original message]
|