How do I match accented characters with PHP preg?

前端 未结 3 1645
忘了有多久
忘了有多久 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);
    

提交回复
热议问题