How do I match accented characters with PHP preg?

Maurice picture Maurice · Jan 25, 2010 · Viewed 14.3k times · Source

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 use symbols like “!”, “@”, "%”, etc.

Is there a way to write a regex to accomplish this? (Preferably without specifying each special letter.)

Now I have:

$reg = '/^[\w\-]*$/';

Answer

Gumbo picture Gumbo · Jan 25, 2010

You could use Unicode character properties to describe the characters:

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

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