php 如何在php中删除字符串中的“\n”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16477098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:12:00  来源:igfitidea点击:

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

php

提问by mingfish_004

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.

如何删除字符串中的“\n”?
我使用了下面的 3 种方法,接缝只有“\s+”有效,但我只想删除 \n,而不是所有空间,如何完成这项工作?
需要帮助,这个问题困扰了我很长时间。

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

//$str=str_replace("\n","",$str);
$str=preg_replace("@\n@","",$str);
//$str=preg_replace("@\s+@"," ",$str);  
echo $str;

回答by Serhiy

This should also do the trick.

这也应该可以解决问题。

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

回答by Hosam alzagh

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

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

上面的代码将用一个空格替换多个空格和换行符。