$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 = preg_replace('~[\r\n\t]+~', '', $text);

其他回答

为了提高速度,应该使用str_replace,并对数组使用双引号

str_replace(array("\r\n","\r"),"",$string);

以下代码工作所有文本请使用它:

$des = str_replace('\n',' ',$des);
$des = str_replace('\r',' ',$des);

用这个:

将换行符替换为空字符串:

$string = preg_replace("/[\\n\\r]+/", "", $string);

或者你可能想用一个空格替换换行符:

$string = preg_replace("/[\\n\\r]+/", " ", $string);

您可以尝试下面的代码将保留文本中的任何空白和新行。

$str = "
put returns between paragraphs

for linebreak add 2 spaces at end

";

echo preg_replace( "/\r|\n/", "", $str );

非常简单的

$hello = "
A
B
C
";
str_replace("
", " ", $hello);
// A B C