W3docs

How to remove line breaks (no characters!) from the string?

There are a few different ways to remove line breaks from a string in PHP. Here are two options.

There are a few different ways to remove line breaks from a string in PHP. Here are two options:

  1. Use the str_replace function:

How to remove line breaks from a string in PHP?

php— editable, runs on the server

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

  1. Use the preg_replace function with a regular expression that matches line breaks:

How to remove line breaks from a string in PHP with a regular expression?

php— editable, runs on the server

Note that the regular expression /[\r\n]+/ matches both Windows-style (\r\n) and Unix-style (\n) line breaks. These examples replace line breaks with a space. To remove them entirely, replace ' ' with ''.