How to Remove Line Breaks from a String in PHP?

Problem:

Suppose you’ve got a long text message from user input. When you display it properly(using nl2br() function) in PHP, it adds line breaks(see below). If you see the browser’s source code, you find that the browser has added <br> or <br /> tag at the end of each line. Now, you want to remove all those line breaks and print all the texts in one line. So, there would be no <br > or <br /> tag.

“Dear admin
I may not have always shown it, but I’m grateful to have had such an awesome teacher like you.

Thank you for all the wisdom you shared and setting a great example.”

Solution:

Depending on the system you’re using, a line break in PHP is represented by the following escaped characters

  • On Linux – \n
  • On MAC – \r
  • On Windows – \r\n

When a user presses enter key while he is writing, PHP returns one of the above three line break escaped characters depending on the system. And when you convert that PHP text into HTML(using nl2br() function), PHP interpreter will add <br> or <br /> in place of each line break.

To remove line breaks, you’ll remove those escaped characters from the string. Choose any of the following two methods to accomplish it-

Method 1: Using str_replace() function

The following code use str_replace() function to remove any line break existed in your supplied text-

<?php
$str = "Dear admin
I may not have always shown it, but I'm grateful to have had such an awesome teacher like you.

Thank you for all the wisdom you shared and setting a great example.
";
$str = str_replace(array("\r","\n"), "", $str);
echo nl2br($str);
?>

[wpdm_file id=168]

Output:
Dear admin I may not have always shown it, but I’m grateful to have had such an awesome teacher like you.Thank you for all the wisdom you shared and setting a great example.

How it works:

Line 2 The string that the $str variable representing has few line breaks. PHP will represent each line break as \n or \r or combination of both.
Line 3 The str_replace() function searches all the occurences of each element of array(which is the first parameter) in the third parameter(which is the supplied string) and replaces with nothing(which is specified by the second parameter – “”). And, returns the new string.
Line 4 As now there is no excaping sequence exists in the string, the nl2br() function won’t insert any HTML <br /> tag in the output, so it will be an one line string output.

 

Method 2: Using preg_replace() function

PHP’s preg_replace() function performs a regular expression search and replace which can be used to remove line breaks from a string.

See how it works in the following example –

<?php
$str = "Dear admin
I may not have always shown it, but I'm grateful to have had such an awesome teacher like you.

Thank you for all the wisdom you shared and setting a great example.
";
$str = preg_replace("#\r|\n#", "", $str);
echo nl2br($str);
?>

[wpdm_file id=169]

Output:
Dear admin I may not have always shown it, but I’m grateful to have had such an awesome teacher like you.Thank you for all the wisdom you shared and setting a great example.

How it works:

Line 2 The $str variable holds the string that we’ll work on.
Line 7 The preg_replace() function searches in the third parameter $str to match the first parameter(which is a regular expression) and replace with the second parameter(which is nothing – “”).Now, let’s examine what the regular expression in the first parameter means-
The preg_replace() function requires you to enclose the regular expression by a delimiter. Here, hash(#) tags are the delimiers(Two other commonly used delimiters are – \(backslash) and `(tildi)). You can use anything as a delimiter as long as it is not a alphanumeric, backslash, or whitespace character.As we’re looking for \r or \n escaped characters, we mention the “OR” as the vertical bar or pipe symbol(|).
Line 8 As any \r or \n is now removed from the string($str), the nl2br() function won’t insert any HTML <br /> tag in the output, so it will be an one line string output.