Only uppercase and lowercase letters and accents in regular expressions [duplicate]


How to create a regular expression in the right way where it accepts only uppercase and lowercase letters, along with accents?

This is to validate a string name, I created it as follows:

$String  = preg_replace("/([^a-zà-úA-ZÀ-Ú ])/", "", $String);

It works perfectly but with a however, since it comes with the following characters: äåæËÎÏÐðÑ × ÷ ØÝÞß

Is there another way to create without these characters coming together?

asked by anonymous 24.12.2017 / 07:10

1 answer


You can include a conditional | followed by the characters between [] you want to include in the search:

/([^a-zà-úA-ZÀ-Ú ]|[äåæËÎÏÐðÑ×÷ØÝÞß])/

If you have problems with accentuation in preg_replace , enter the flag u (Unicode):

/([^a-zà-úA-ZÀ-Ú ]|[äåæËÎÏÐðÑ×÷ØÝÞß])/u



24.12.2017 / 17:22