Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Remove new lines from string in PHP

  • Last Updated : 20 Oct, 2018

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()




<?php
$text = "Hello \nwelcome to \ngeeksforgeeks";
echo $text;
  
echo "\n";
$text = str_replace("\n", "", $text);
echo $text;
?> 
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!