$string = "
put returns between paragraphs
for linebreak add 2 spaces at end
";
想从字符串中删除所有新行。
我有这个正则表达式,它可以捕获所有的,问题是我不知道该用哪个函数来使用它。
/\r\n|\r|\n/
$string应该变成:
$string = "put returns between paragraphs for linebreak add 2 spaces at end ";
$string = "
put returns between paragraphs
for linebreak add 2 spaces at end
";
想从字符串中删除所有新行。
我有这个正则表达式,它可以捕获所有的,问题是我不知道该用哪个函数来使用它。
/\r\n|\r|\n/
$string应该变成:
$string = "put returns between paragraphs for linebreak add 2 spaces at end ";
当前回答
转义序列\R匹配通用换行符
也就是说,任何被Unicode视为换行序列的东西。这包括由\v(垂直空格)匹配的所有字符,以及多字符序列\x0D\x0A…
$string = preg_replace('/\R+/', " ", $string);
在8位非utf -8模式下\R相当于以下内容:(?>\ R \n|\n|\x0b|\f|\ R |\x85)…pcre.org
Regex101演示
其他回答
文本中的换行符通常表示为:
\r\n -在Windows电脑上
\r -在苹果电脑上
\n - Linux
//Removes all 3 types of line breaks
$string = str_replace("\r", "", $string);
$string = str_replace("\n", "", $string);
只需使用preg_replace()
$string = preg_replace('~[\r\n]+~', '', $string);
你可以在这个问题上使用str_replace(),尽管代码看起来不那么干净:
$string = str_replace(array("\n", "\r"), '', $string);
在脑海中看到它
也许这样行得通:
$str='\n';
echo str_replace('\n','',$str);
使用上述解决方案的组合,这条线对我来说很有效
$string = trim(str_replace('\n', '', (str_replace('\r', '', $string))));
它删除了“\r”和“\n”。
非常简单的
$hello = "
A
B
C
";
str_replace("
", " ", $hello);
// A B C