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!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

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.

 

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.

Buy Me A Coffee Code Boxx eBooks

 

 

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”.

 

 

EXTRAS

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

 

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 *