HTML Purifier newlines to <br/> elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

HTML Purifier newlines to <br/> elements

Post 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.
nmreddy
Forum Commoner
Posts: 25
Joined: Wed Feb 18, 2009 5:36 am

Re: HTML Purifier newlines to <br/> elements

Post by nmreddy »

May be this will help

nl2br (php function) -- converts \n to <br> ..

<p> i don't have an idea ..
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: HTML Purifier newlines to <br/> elements

Post 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! :D
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: HTML Purifier newlines to <br/> elements

Post by JellyFish »

Actually I don't want to convert newlines that are in html like:

Code: Select all

<p>
hello world
</p>
I don't want to place <br/> tags within the p element.

Code: Select all

<p><br/>hello world<br/></p>
Is there a way to check if there is any HTML in the body before I use nl2br?
Last edited by Benjamin on Tue May 19, 2009 2:27 pm, edited 1 time in total.
Reason: Changed code type from text to html.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: HTML Purifier newlines to <br/> elements

Post by John Cartwright »

Use regex
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: HTML Purifier newlines to <br/> elements

Post 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?
Last edited by Benjamin on Tue May 19, 2009 2:27 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply