Welcome! » Log In » Create A New Profile

nl2br, auto <br /> insertion

Posted by matthew 
matthew
nl2br, auto <br /> insertion
February 17, 2009 02:53AM

Hello, Is it possible to have something like nl2br functionality in Purifier?

AutoParagraph works excellently, but it would be great if it could also recognize new line from textarea and convert it into tag.

Greetings

Re: nl2br, auto <br /> insertion
February 17, 2009 10:25AM

No, not yet. This is a little bit my personal agenda at work, because I don't think people should be nl2br()ing text. Would you be interested in implementing it?

matthew
Re: nl2br, auto <br /> insertion
February 17, 2009 02:02PM

Temporarily I use this workaround:

$content = paragraph($purifier->purify( $content ));

where paragraph is a function taken from Smarty modifier http://tinyurl.com/bdhgh2 (converting double newline to <p> tag)

Pip
Re: nl2br, auto <br /> insertion
July 14, 2009 12:38PM

One solution I've had success with is a simple regex parse. The actual implementation will depend on your page layout, but it's pretty simple.

function lineBreakToParagraph($string)
{
    return preg_replace("/(\r\n)+|(\n|\r)+/", "</p><p>", $string);
}

This works well for me as an auto-paragraph because I'm using it to insert content between <p></p> tags anyways so simply closing them and opening a new one is enough to not break page flow. Your implementation will almost certainly require something similar but slightly different.

Re: nl2br, auto <br /> insertion
July 17, 2009 08:23AM

personally, with the amount of problems and feedback we've had from people in our project, clients and businesses etc.. forcing nl2br has been a big major pain in the rear. especially when you offer the use of multiple wysiwyg editors.. some have nl2br enabled & some don't. and all the feedback we got was that nl2br is a bad idea, because it screws layouts up and can mess with the format of the page (with linespaces and so on.) and then when you use paragraphs to seperate paragraphs which then creates extra whitespace between paragraphs..

needles to say though you could as you have done pre process the $html before you purify

preg_replace("/(\015\012)|(\015)|(\012)/","<br />",$text);

but when dealing with html content & editors, you want nl2br disabled & only use it with plaintext (though with plaintext you shouldn't be using purifier)

Sorry, you do not have permission to post/reply in this forum.