$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 ";
当前回答
$string = str_replace(array("\n", "\r"), ' ', $string);
其他回答
文本中的换行符通常表示为:
\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替换PCRE正则表达式: http://php.net/manual/en/function.preg-replace.php
$new_string = preg_replace("/\r\n|\r|\n/", ' ', $old_string);
将用空格替换新行或返回字符。如果你不想用任何东西替换它们,将第二个参数改为“。
为了提高速度,应该使用str_replace,并对数组使用双引号
str_replace(array("\r\n","\r"),"",$string);
非常简单的
$hello = "
A
B
C
";
str_replace("
", " ", $hello);
// A B C
我不确定这是否对已经提交的答案有任何价值,但我也可以张贴它。
// Create an array with the values you want to replace
$searches = array("\r", "\n", "\r\n");
// Replace the line breaks with a space
$string = str_replace($searches, " ", $string);
// Replace multiple spaces with one
$output = preg_replace('!\s+!', ' ', $string);