You are here: Re: create_text_node + newlines (chr13 + <br />) « All PHP « IT news, forums, messages
Re: create_text_node + newlines (chr13 + <br />)

Posted by exhuma.twn on 12/27/06 10:08

Janwillem Borleffs wrote:
> exhuma.twn wrote:
> > I am having trouble inserting text containing newlines as text nodes.
> > It replaces the newlines with proper entities (&#13;) and <br/>'s with
> > "&lt;br /&gt;". 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

Thanks. This solved the problem. And as the data I am inserting comes
from a plain-text email anyhow, well-formedness is not a problem.

Thanks again.

--
Mich

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация