Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression that allows letters (like "ñ") from any language

trying to let users use special characters in other languages such as Spanish or French. I originally had this:

 "/[^A-Za-z0-9\.\_\- ]/i" 

and then changed it to

 "/[^\p{L}\p{N}\.\_\-\(\) ]/i" 

but still doesn't work. letters such as "ñ" should be allowed. Thanks.

Revision: I found that adding a (*UTF8) at the beginning helps solve the problem. So I'm using the following code:"/(*UTF8)[^\p{L}A-Za-z0-9._- ]/i"

Revision: After looking at the answers I decided to use: "/[^\p{Xwd}. -]/u". Thanks(It works even with the Chinese alphabet.

like image 566
user3357607 Avatar asked Feb 26 '14 20:02

user3357607


1 Answers

for latin languages you can use the \p{Latin} character class:

/[^\p{Latin}0-9._ -]/u

But if you want all other letters and digits:

/[^\p{Xwd}. -]/u

The "u" modifier indicates that the string must be read as an unicode string.

like image 163
Casimir et Hippolyte Avatar answered Oct 03 '22 05:10

Casimir et Hippolyte