3 Ways To Remove Line Breaks In PHP – Simple Examples

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:

  1. $nobreak = str_replace(["\r", "\n"], "", $STRING);
  2. $nobreak = preg_replace("/\r|\n/", "", $STRING);
  3. $nobreak = trim($STRING);

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Remove Line Breaks Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

REMOVE LINE BREAKS

All right, let us now get into the ways to remove line breaks in PHP.

 

METHOD 1) STRING REPLACE

1-replace.php
<?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 (\r and \n) with an empty string, effectively removing all of them.

 

METHOD 2) REGULAR EXPRESSION

2-regex.php
<?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

3-trim.php
<?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

4-html-breaks.php
<?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> or <br/>), we do the same old “replace all <br><br/> with an empty string”.

 

 

USEFUL 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

Remove Line Breaks In PHP (click to enlarge)

 

THE END

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!

Leave a Comment

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