Ashburn, February 22, 2018

How to remove line breaks from a string

Sometimes when saving user input from an HTML textarea to a MySQL database and it is later retrieved from the database to be displayed in the front end, it display with line breaks. The problem occurs when users submit text hitting the Enter key at the end of lines. It issue can also arise within a WordPress environment.

How to remove line breaks from a string

You should be able to get rid of those line breaks by replacing them with a preg that removes all newlines and carriage returns. The code is:

$yourNewString = preg_replace( "/\r|\n/", "", $yourString );

Even thought the \n characters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.

Cheers!

About Karlos Maldonado 41 Articles
Computer Sciences Engineer graduated from Florida Atlantic University (FAU) in Boca Raton, Florida. Has gotten great experience in the IT and Web Development industry working for companies such as SIRIUS XM RADIO, 4MEDIA as well as collaborating with local companies.

Be the first to comment

Leave a Reply

Your email address will not be published.