Matching UTF Characters with preg_match in PHP: (*UTF8) Works on Windows but not Linux

后端 未结 3 491
别那么骄傲
别那么骄傲 2020-12-10 07:16

I have a simple regular expression to check a username:

preg_match(\'/(*UTF8)^[[:alnum:]]([[:alnum:]]|[ _.-])+$/i\', $username);

In local t

3条回答
  •  生来不讨喜
    2020-12-10 08:11

    it seems it is an old post but as it is always a subject of interest I will post what I discovered here. It is a small difference but makes code more simple. The thing is that curly brackets are optional.

    The above code of Gumbo and Scott can be written more simple like this if someone wants to allow only letters (Unicode & non-Unicode) and blank spaces:

    preg_match("/^\pL[\pL ]+$/u",$string)
    

    I also noticed that preg_match accepts even more simple code as the following :

    preg_match("/^[\pL ]+$/u",$string)
    

提交回复
热议问题