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条)

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗