20

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\-]*$/';
TRiG
  • 10,148
  • 7
  • 57
  • 107
Maurice
  • 4,829
  • 7
  • 41
  • 50

3 Answers3

41

You could use Unicode character properties to describe the characters:

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

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

Gumbo
  • 643,351
  • 109
  • 780
  • 844
5

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);
Matteo Riva
  • 24,728
  • 12
  • 72
  • 104
4

you can try with this regex:

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

which catch also accented characters

Manuel
  • 836
  • 13
  • 30