Php 是否删除textarea表单中的两个以上回车?

Php 是否删除textarea表单中的两个以上回车?,php,textarea,preg-replace,carriage-return,nl2br,Php,Textarea,Preg Replace,Carriage Return,Nl2br,我花了几个小时研究了几十种不同的解决方案,但没有一种有效。我收到一个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 n

我花了几个小时研究了几十种不同的解决方案,但没有一种有效。我收到一个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___替换吗?请注意,我不想修改数据(请不要使用nl2br()),因为我更容易保留原始数据(ios支持)。

您可以尝试

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

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

也许您应该尝试以下方法:

preg_replace('/\n\r(\n\r)+/', "\n\r", $str);

这是怎么回事:
/\n(\n)+/g
我已经尝试用\n\n替换/\n\n+/,但没有成功。。。我不确定文本区域中新行和返回的字符是什么…这阻止了示例1(确定)的发生,因为这两行之间不再有空行。我试过使用/[\r\n]{2,}/但是相同……在这两种情况下都应该有效。你确定除了换乘外,你不需要换乘马车吗?不知道为什么会这样,但它是有效的。虽然preg_replace('/[\n\r]{2,}/',“\n\r”,$str),但我发现这样更清楚;谢谢!此表达式查找多个新行,并用一个新行替换结尾处的第二行。也欢迎你!