Page 1 of 1
HTML Purifier newlines to <br/> elements
Posted: Mon May 18, 2009 11:49 pm
by JellyFish
Hey, has anyone on devnet worked with HTML Purifier before? I was wondering if there was a way to convert all newline characters (\n) to break and paragraph tags (<br> and <p>)? I know there is a directive called AutoParagraph that will allow me to convert all double newlines to paragraph elements. But what I'm looking to do is convert all single newlines to break elements along with the AutoParagraph directive functionality.
Is there a way to do this with HTMLPurifier or would I have to do it with str_replace?
Thanks for reading. All help is appreciated.
Re: HTML Purifier newlines to <br/> elements
Posted: Tue May 19, 2009 12:51 am
by nmreddy
May be this will help
nl2br (php function) -- converts \n to <br> ..
<p> i don't have an idea ..
Re: HTML Purifier newlines to <br/> elements
Posted: Tue May 19, 2009 1:15 am
by JellyFish
nmreddy wrote:May be this will help
nl2br (php function) -- converts \n to <br> ..
<p> i don't have an idea ..
Hmm... Forgot about that function. Maybe this is all I need though.
Thanks!

Re: HTML Purifier newlines to <br/> elements
Posted: Tue May 19, 2009 11:47 am
by JellyFish
Actually I don't want to convert newlines that are in html like:
I don't want to place <br/> tags within the p element.
Is there a way to check if there is any HTML in the body before I use nl2br?
Re: HTML Purifier newlines to <br/> elements
Posted: Tue May 19, 2009 1:06 pm
by John Cartwright
Use regex
Re: HTML Purifier newlines to <br/> elements
Posted: Tue May 19, 2009 2:21 pm
by JellyFish
I found:
Code: Select all
$string = $purifier->purify($string);
if (strlen($string) == strlen(strip_tags($string)))
{
$string = "<pre>$string</pre>";
}
works. But would using a regular expression be faster then strip_tags?
[EDIT] I had to put the wrap="wrap" attribute into the pre element so that text automatically breaks. Is there a way to do this in css?