by @codeexamples

Removing the new lines from a string in PHP

Home » php » Removing the new lines from a string in PHP

To remove the new lines from a string and replace with a single space, we can use the trim() and preg_replace() functions by passing the regex /\s+/ as an argument in PHP.

Here is an example:

<?php

$string = "
You can
see me
in a single line
";

$string = trim(preg_replace('/\s+/', ' ', $string));


echo $string;

?>

Output:

You can see me in a single line