How to match exact word anywhere in string with PHP regexp

后端 未结 4 710
醉梦人生
醉梦人生 2021-01-27 11:15

I want to find out if user has used the words admin or username anywhere in their possible username string.

So if user wants to use admin

4条回答
  •  轮回少年
    2021-01-27 11:54

    Just look for words using word boundaries:

    /\b(?:admin|username)\b/i
    

    and if there is a match return error e.g.

    if (preg_match('/\b(?:admin|username)\b/i', $input)) {
        die("Invalid Input");
    }
    

提交回复
热议问题