$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('@[\s]{2,}@',' ',$string);

其他回答

也许这样行得通:

$str='\n';
echo str_replace('\n','',$str);

您可以删除新行和多个空白。

$pattern = '~[\r\n\s?]+~';
$name="test1 /
                     test1";
$name = preg_replace( $pattern, "$1 $2",$name);

echo $name;

这些解决方案中有许多对我不起作用。 这招奏效了:-

$svgxml = preg_replace("/(*BSR_ANYCRLF)\R/",'',$svgxml);

参考文献如下:- PCRE和New Lines

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

$str = "
put returns between paragraphs

for linebreak add 2 spaces at end

";

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

可以使用preg_replace替换PCRE正则表达式: http://php.net/manual/en/function.preg-replace.php

$new_string = preg_replace("/\r\n|\r|\n/", ' ', $old_string);

将用空格替换新行或返回字符。如果你不想用任何东西替换它们,将第二个参数改为“。