Home » Php » how can i remove "\n" in string in php?

how can i remove "\n" in string in php?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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("@\[email protected]","",$str);
//$str=preg_replace("@\[email protected]"," ",$str);  
echo $str;
How to&Answers:

This should also do the trick.

$str=str_replace("\r\n","",$str);

Answer:

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

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