在一个新行中替换多个新行


Replace Multiple New Lines in One New Line

我想将多个换行字符替换为一个换行字符,多个空格替换为一个空格并插入到数据库中。
到目前为止,我尝试了这个preg_replace("/'n'n+/", "'n", $text);和不工作!

我也在$text上做这项工作用于格式化。

    $text = wordwrap($text,120, '<br/>', true);
    $text = nl2br($text);

尝试使用以下模式:

/['n'r]+/

如下:

preg_replace( "/['r'n]+/", "'n", $text );

你可能需要这个:

preg_replace("/('s)+/", "$1", $input_lines);

's——匹配任何空白字符(所有字符,如空格、制表符、新行等)

$1——集合中的第一个空白字符。如果第一个是空格,后面有3行新行。我们只能得到1个空格

你也可以这样写:

preg_replace("/('n)+/", "$1", $input_lines);
preg_replace("/( )+/", "$1", $input_lines);

您需要根据系统使用正确的行结束符。PHP_EOL为您决定行结束符

$text = str_replace(PHP_EOL, array("'n", "'r'n", "'r"), $text);

<br />仅用于HTML

Windows使用"'r'n"表示新的行字符

基于unix的使用"'n"

Mac(我想)使用"'r"

试着使用以下语句:

$text = str_replace(PHP_EOL, '', $text);

考虑到最后一行的空格。$text= preg_replace( "/['r'n]+'s+/", "'r'n", $text );

try preg_replace():

preg_replace("/['r'n]+/", "'n", $text);