PHP Regex validate letters and Spanish accent

前端 未结 3 835

How can I add/improvised my code so Spanish accent will be considered as valid in addition to normal alphabet (a-z)

I have the following in my code

p         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 15:22

    As found in the answer to this question, you could match accented characters using the full Letter Unicode property \p{L}. That includes regular a-z characters along with accented ones, so replace a-z in your expression with this like so:

    $reg = "#[^\p{L}\s-]#u";
    

    Note that to use this you need the UTF-8 modifier u after your closing delimiter, as the docs say such Unicode "character types are available when UTF-8 mode is selected".

提交回复
热议问题