$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 ";

当前回答

非常简单的

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

其他回答

用这个:

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

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

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

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

非常简单的

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

也许这样行得通:

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

使用上述解决方案的组合,这条线对我来说很有效

 $string = trim(str_replace('\n', '', (str_replace('\r', '', $string))));

它删除了“\r”和“\n”。

我不确定这是否对已经提交的答案有任何价值,但我也可以张贴它。

// 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);