PHP Alphanumeric RegExp with letter from any language
Discuss coding issues, and scripts related to PHP and MySQL.
-
Marius
- Posts:107
PHP Alphanumeric RegExp with letter from any language
I am trying to write a Regular Expression that makes sure the characters are all Alpha Numeric, and that accepts other characters as well. I would like to accept letters from other languages: áéíñóúüÁÉÍÑÓÚÜ and @.+-.
To accept all alphanumeric characters, and the symbols @.+-. I use the expression:
Code: Select all
if (!preg_match("!^[\w@.-]*$!", $str)) {
echo 'error';
}
Now I want to grab the characters
áéíñóúüÁÉÍÑÓÚÜ as well. When I use the expression:
Code: Select all
if (!preg_match("/[\wáéíñóúüÁÉÍÑÓÚÜ@.\+-].*/", $str)) {
echo 'error';
}
It does not seem to work, could anyone please help?
MarPlo
Posts:186
Hi,
Your regex
^[\w@.-]*$ won't match the accented characters. That is \w would match only the English alphabets, digits 0-9 plus the underscore symbol.
-
\p{L} matches any kind of letter from any language
-
\p{N} matches any kind of numeric character in any script.
- The + after the char class would repeat the previous token one or more times. I suggest you to use + instead of * because * repeats the previous token zero or more times. So it matches empty strings also.
You must need to include the unicode modifier u, so that it would make \p{L} to match accented characters.
Regex to accept all alphanumeric characters, and the symbols @.+_- and space.
Code: Select all
if (!preg_match("/^[\p{L}\p{N}@ \+._-]+$/u", $str)) {
echo 'error';
}
Or try this RegExp:
Code: Select all
/^[A-z0-9 áéíñóúüÁÉÍÑÓÚÜ@.\+_-]+$/u