Friday, October 24, 2025
HomeLanguagesRemove new lines from string in PHP

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 neveropen.
Output : Hello welcome to neveropen.
Remove the new line between Hello and neveropen.

Input : I
        love
        neveropen
Output : I love neveropen

The idea is to use str_replace()




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


RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS