0

我花了几个小时研究了几十种不同的解决方案,但没有一个奏效。我在 php 字符串中接收到 textarea 的内容,我想删除超过一个空行的任何内容。

Example ok
Hi how are you, 
// one blank line here so ok to keep
Not too bad thanks

Example not ok
hi how are you
// two lines (or more) here so we remove one and keep the other

not too bad thanks

有人知道要使用的正确 preg_replace 吗?请注意,我不想修改数据(请不要使用 nl2br()),因为我更容易保持原始数据(ios 支持)。

4

2 回答 2

0

你可以试试

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

它用一个换行符替换一个(或多个)换行符或回车符。

于 2015-09-03T10:25:07.837 回答
0

也许你应该试试这个:

preg_replace('/\n\r(\n\r)+/', "\n\r", $str);
于 2015-09-03T10:34:40.057 回答