Skip to content
Related Articles

Related Articles

Remove new lines from string in PHP

Improve Article
Save Article
  • Last Updated : 20 Oct, 2018
Improve Article
Save Article

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


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

Input : I
Output : I love geeksforgeeks

The idea is to use str_replace()

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!