RSS
Home php PHP nl2br on one line
formats

PHP nl2br on one line

I usually store data from a textarea directly to the database as is, then once I retrieve it back into HTML I use that lovely PHP function nl2br() to convert it to HTML
tags.

This works well for most cases until you are passing this data back into javascript where everything has to be on one line!

nl2br() outputs as follows:

Line one

Line two

And I want it to be as follows:

Line one
Line two

So how do I do this?

echo trim(strtr($myOriginalString, array("\r\n" => "<br />", "\r" => "<br />", "\n" => "<br />")));
 
Tags: ,
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is the official blog of HSMoore Ltd.

© Site by HSMoore Ltd