Remove new lines from string in PHP

Given a multiple line sentence or statement the task is to convert the whole statement in a
single line. See the example below.

Examples:

Input : Hello
        welcome to geeksforgeeks.
Output : Hello welcome to geeksforgeeks.
Remove the new line between Hello and geeksforgeeks.

Input : I
        love
        geeksforgeeks
Output : I love geeksforgeeks

The idea is to use str_replace()

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$text = "Hello \nwelcome to \ngeeksforgeeks";
echo $text;
  
echo "\n";
$text = str_replace("\n", "", $text);
echo $text;
?> 

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.