php 正则表达式,确保用户不会添加太多新换行符
在我的网站上输入表格以征求意见。我有一些愚蠢的用户通过按 Enter [空格] 输入 [空格] 或大量输入等创建了大量空白 在 PHP 中,如何保留合法的单输入或双输入“输入”,但删除中间有空格的任何其他组合。 例如,这可以:
\n\n
这不是:
\n\n\n...
\n\n \n...
\n \n \n...
Input form on my website for comments. I have some silly users creating a whole lot of blank whitespace by pressing enter [space] enter [space] or lots of enters etc
In PHP, how do I preserve legit single enter or double 'enter enter', but remove any other combinations with spaces inbetween.
eg, This is OK:
\n\n
This is NOT:
\n\n\n...
\n\n \n...
\n \n \n...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
句柄:
\n
(注意\s
是[ \r\t\n]
因此此模式包括任何匹配\n{3,}
)\n
,中间有空格,后跟另一个\n
。\s+
是贪婪的,因此会尽可能多地获取中间空格和\n
。请注意,此正则表达式会替换字符串中至少包含两个
\n
的任何部分,但\n\n
(您希望保留的)除外。编辑(感谢@AlanMoore,请参阅下面的评论):它将这些“太多\n”事件替换为单个
\n
,而不是将它们作为您原来的问题完全删除指定的。这是因为如果输入字符串是foo\n\n\nbar
,那么您似乎宁愿使用foo\nbar
而不是foobar
作为结果字符串。如果您确实想完全删除所有这些事件,只需替换为上面的
''
而不是\n
。handles:
\n
in a row (note\s
is[ \r\t\n]
so this pattern includes anything that matches\n{3,}
)\n
with whitespace in between followed by another\n
. The\s+
is greedy so will grab as many intermediate spaces and\n
as it can.Note that this regex replaces any part of the string that contains at least two
\n
s, except for\n\n
(which you wanted to be preserved).EDIT (thanks to @AlanMoore, see comments below): It replaces these "too many \n" occurences with a single
\n
rather than removes them entirely as your original question specified. This is because if the input string wasfoo\n\n\nbar
it seems like you'd rather havefoo\nbar
thanfoobar
as the result string.If you do indeed want to remove all these occurences entirely, just replace with
''
above instead of\n
.终于让它工作并测试了。我决定通过两次替换来打破该过程,以保持正则表达式代码简单(如果您愿意,可以将两者结合起来)。
第一个 (
/\n | {1,} | \n/
) 将查找空格和\n
字符的任意组合,留下单独的\ n
。第二个 (
'/(?<=\n{2})\n*/'
) 使用后视功能来匹配任何\n
字符组跟在任何“\n\n”(双换行序列)之后。测试:
输出:
我不是正则表达式专家,但我认为它已经很好地解决了问题。
Finally got it working and tested. I decided to break the process in two passes of replaces to keep the regex code simple (you can combine both if you desire).
First one (
/\n | {1,} | \n/
) will look for any random combination of spaces and\n
characters, leaving the solo\n
.Second one (
'/(?<=\n{2})\n*/'
) makes use of look behind feature to match any group of\n
characters that follows any '\n\n' (double new line sequence).Testing:
Output:
I'm not a regex guru, but I think it already solves the problem decently.
查找后跟 1 个或多个空白字符(空格、制表符等)的任何换行符,并将其替换为单个换行符。
Find any linebreak followed by 1 or more whitespace characters (spaces, tabs, etc..) and replace it with a single linebreak.