i am building a system in which user comment and earn credit. to get credit quickly user add comment such as "fffff" , "niceeeeeeeeee", "greeeeeeaaaatt", "aaaa", "b" etc...
Is there anyway to filter out these comments. any suggestion will be appreciated.
在php中过滤正确的单词和适当的语言英语
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- dqeonr8554 2016-05-10 05:53关注
You could check to see if the user's input contains 3 consecutive characters using a regular expression (since I'm not aware of any words in english which have 3 of the same letter in a row)
$user_input = "niceeeeeeeeeeee"; if (preg_match("/([A-Za-z])\\1\\1/", $user_input)) { echo "String contains the same letter 3 times in a row and is not valid"; } else { echo "String is ok!"; }
This would match "niceee", "greeeat", "aaaa" etc. or any string with the same letter 3 or more times in a row. If you want to check users' input against multiple patterns, you could put your regular expressions in an array and check them all e.g.:
$patterns = [ "/(.)\\1\\1/", // any character (not just letters) 3+ times in a row "/^.$/", // a single character "/.{15,}/", // contains a word longer than 15 characters "/([A-Za-z]{2,})\\1\\1/" // 2 letters alternating e.g. "abababab" ]; foreach( $patterns as $pattern ){ if (preg_match($pattern, $user_input)) { echo "This is an invalid string"; } }
Or if you don't have too many patterns (and you aren't concerned with readability) you could just concatenate all the patterns together with
|
.if (preg_match("/(.)\\1\\1|^.$|.{15,}|([A-Za-z]{2,})\\2\\2/", $user_input)) { echo "This is an invalid string"; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 数学建模,尽量用matlab回答,论文格式
- ¥15 昨天挂载了一下u盘,然后拔了
- ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
- ¥20 易康econgnition精度验证
- ¥15 msix packaging tool打包问题
- ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
- ¥15 python的qt5界面
- ¥15 无线电能传输系统MATLAB仿真问题
- ¥50 如何用脚本实现输入法的热键设置
- ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能