tags:

views:

313

answers:

3

Hi,

I'd like to give my users the option to not only fill in letters and numbers, but also "special" letters like the "á", "é" etc. Though 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\-]*$/';

Thanks in advance!

A: 

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);
kemp
doesn't seem to work either. But Gumbo's solution worked. Thanks for the reply
Maurice
A: 

you can try with this regex:

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

which catch also accented characters

EndelWar
doesn't seem to work on my server. But Gumbo's answer worked. Thanks for the reply
Maurice
+3  A: 

You could use Unicode character properties to describe the characters:

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

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

Gumbo
Seems to do the trick. Thanks for the quick reply!!
Maurice