I can't get preg_match to test if the entire string matches the regex

后端 未结 2 1981
旧时难觅i
旧时难觅i 2021-01-20 16:43

I\'m using this regular expression to test if a username is valid:

[A-Za-z0-9 _]{3,12} when I test it for matches in a text editor with the string

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 17:12

    You should add start and end anchors (^$):

    if(!preg_match('/^[A-Za-z0-9 _]{3,12}$/', $content)
    

    The anchor ^ matches the start of the string and $ matches the end. That way, it will only match if the whole string satisfies your regex.

    Hope that helps

提交回复
热议问题