Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i remove "\n" in string in php? [duplicate]

Tags:

php

how to remove "\n" in string?
I used 3 method bellow ,seams that only the "\s+" works , but I only want to remove \n,not all space ,how to do the job?
need help, this question had puzzled me for a long time.

$str="<p>
    hello<br>
    world
</p>";

//$str=str_replace("\n","",$str);
$str=preg_replace("@\n@","",$str);
//$str=preg_replace("@\s+@"," ",$str);  
echo $str;
like image 724
mingfish_004 Avatar asked May 10 '13 07:05

mingfish_004


2 Answers

This should also do the trick.

$str=str_replace("\r\n","",$str);
like image 145
Serhiy Avatar answered Sep 22 '22 16:09

Serhiy


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

The above code will replace multiple spaces and newlines with a single space.

like image 33
Hosam Elzagh Avatar answered Sep 20 '22 16:09

Hosam Elzagh