PHP nl2br on one line

0 min read 94 words

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" => "", "\r" => "", "\n" => "")));
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags