$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 ";
当前回答
为了提高速度,应该使用str_replace,并对数组使用双引号
str_replace(array("\r\n","\r"),"",$string);
其他回答
以下代码工作所有文本请使用它:
$des = str_replace('\n',' ',$des);
$des = str_replace('\r',' ',$des);
可以使用preg_replace替换PCRE正则表达式: http://php.net/manual/en/function.preg-replace.php
$new_string = preg_replace("/\r\n|\r|\n/", ' ', $old_string);
将用空格替换新行或返回字符。如果你不想用任何东西替换它们,将第二个参数改为“。
您可以尝试下面的代码将保留文本中的任何空白和新行。
$str = "
put returns between paragraphs
for linebreak add 2 spaces at end
";
echo preg_replace( "/\r|\n/", "", $str );
为了提高速度,应该使用str_replace,并对数组使用双引号
str_replace(array("\r\n","\r"),"",$string);
我惊讶地发现,每个人对正则表达式知之甚少。
php中的换行符是
$str = preg_replace('/\r?\n$/', ' ', $str);
在perl中
$str =~ s/\r?\n$/ /g;
意思是替换行末的任何换行符(为了效率)-可选在回车符之前-用空格。
\n或\015是换行符。 \r或\012是回车。 ? 在正则表达式中,表示匹配前一个字符的1或0。 在正则表达式中,$表示匹配行尾。
最初最好的正则表达式参考是perldoc perlre,每个程序员都应该非常了解这个doc: http://perldoc.perl.org/perlre.html 注意,并非所有语言都支持所有特性。