Welcome to a quick tutorial on how to remove line breaks in PHP. Need to get rid of all line breaks from a string? Remove some extra ones?
The common ways to remove line breaks in PHP are:
$nobreak = str_replace(["\r", "\n"], "", $STRING);
$nobreak = preg_replace("/\r|\n/", "", $STRING);
$nobreak = trim($STRING);
That should cover the basics, but read on for more examples!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
REMOVE LINE BREAKS
All right, let us now get into the ways to remove line breaks in PHP.
METHOD 1) STRING REPLACE
<?php // (A) STRING WITH LINE BREAKS $str = "Hello World Foo Bar! "; echo $str; // (B) REPLACE LINEBREAKS WITH EMPTY STRING (REMOVE) $nobreak = str_replace(["\r", "\n"], "", $str); echo $nobreak;
This is probably one of the easiest and fastest methods. Just replace all line breaks (
\n) with an empty string, effectively removing all of them.
METHOD 2) REGULAR EXPRESSION
<?php // (A) STRING WITH LINE BREAKS $str = "Hello World Foo Bar! "; echo $str; // (B) REPLACE LINEBREAKS WITH EMPTY STRING (REMOVE) $nobreak = preg_replace("/\r|\n/", "", $str); echo $nobreak;
Next, we have something called the “regular expression”. In simple terms, regular expressions allow us to input our own custom match-and-replace terms, and it can get extremely complicated. So I will just leave this here –
/\r|\n/ is the regular expression to match all line breaks.
METHOD 3) TRIM
<?php // (A) STRING WITH LINE BREAKS $str = " Hello World "; echo $str; // (B) TRIM - REMOVE LEADING & TRAILING LINE BREAKS $nobreak = trim($str); echo $nobreak;
To only remove the leading and trailing line breaks, use the
trim() function instead. Yes, take note,
trim() leaves all line breaks in-between as-it-is.
METHOD 4) REMOVE HTML LINE BREAKS
<?php // (A) HTML LINE BREAKS $str = "Hello<br>World<br/>Foo"; echo $str; // (B) REMOVE HTML LINE BREAKS $nobreak = str_replace(["<br>", "<br/>"], "", $str); echo $nobreak;
Lastly, this should be pretty self-explanatory. To remove HTML line breaks (
<br/>), we do the same old “replace all
<br><br/> with an empty string”.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
INFOGRAPHIC CHEAT SHEET
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!