How do I match accented characters with PHP preg?

前端 未结 3 1612
忘了有多久
忘了有多久 2020-12-01 09:32

I’d like to give my users the option to not only fill in letters and numbers, but also “special” letters like the “á”, “é”, etc. However, I do not want them to be able to us

相关标签:
3条回答
  • 2020-12-01 09:55

    What characters are considered "word-characters" depends on the locale. You should set a locale which has those characters in its natural alphabet, and use the /u modifier for the regexp, like this:

    $str = 'perché';
    setlocale(LC_ALL, 'it_IT@euro');
    echo preg_match('#^\w+$#u', $str);
    
    0 讨论(0)
  • 2020-12-01 10:06

    You could use Unicode character properties to describe the characters:

    /^[\p{L}-]*$/u
    

    \p{L} describes the class of Unicode letter characters.

    0 讨论(0)
  • 2020-12-01 10:13

    you can try with this regex:

    $reg = '~[^\\pL\d]+~u';
    

    which catch also accented characters

    0 讨论(0)
提交回复
热议问题