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"; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?
- ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥15 cmd cl 0x000007b
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
- ¥500 火焰左右视图、视差(基于双目相机)