dongxixiu9134 2017-04-30 08:16
浏览 35
已采纳

正则表达式阻止多个项目

I have a form on a site that is collecting user details. There was a fool submitting the form with a name like "Barneycok" from different IP addresses, so I learned how to block that name from going through on the form.

I learned a little regex, just enough to write this little piece:

if (preg_match('/\b(\w*arneycok)\b/', $FirstName)){
$error.= "<li><font face=arial><strong>Sorry, an error occured.  Please try again later.</strong><br><br>"; 
$errors=1;
}

The code has worked perfectly and I never got that name coming through anymore. However, recently, someone is entering a string of numbers on the name field.

The string looks like this:

123456789
123498568
123477698
12346897w

If you notice, the first 4 characters are constant throughout.

So how do I add that in my regex above so that if the name starts with "1234", it will simply match that regex and give the user the error code?

Your help will be greatly appreciated.

Jaime

  • 写回答

3条回答 默认 最新

  • douxian9010 2017-04-30 08:29
    关注

    This will match $FirstName which starts with 1234. for matching a specific word like Barneycok you should use this (b|B)arneycok

    Regex: ^\s*1234|\b(?:b|B)arneycok\b

    1. ^\s*1234 starts with 1234 can contain spaces in starting

    2. | is like or condition,

    3. \b(?:b|B)arneycok\b matches the word which contains barneycok or Barneycok

    Try this code snippet here

    if (preg_match('/^1234|\b(?:b|B)arneycok\b/i', $FirstName))
    {
        $error.= "<li><font face=arial><strong>Sorry, an error occured.  Please try again later.</strong><br><br>";
        $errors = 1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?