Help?use Regex that acceps both english and greek characters

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Lykos22
Forum Newbie
Posts: 2
Joined: Thu Jul 05, 2012 2:24 am

Help?use Regex that acceps both english and greek characters

Post by Lykos22 »

Hi I would like some help please if its possible.
I' m having a contact form in which the user submits his name. I use both in my javascript and php validation this regex:

/^[A-Z][A-Za-z.'\- ]+$/

for validatin the name, accepting only characters, whitespaces and - symbol. Although it works fine with english (latin characters), it doesn't work with greek. I want my regex to accept both english and greek characters, so when the user submits his name either in english either in greek it will "pass", if he enters any number or symbol like @,!,* etc (exept whitespaces, ' , -) fails.

Any help would be appreciated.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Help?use Regex that acceps both english and greek charac

Post by tr0gd0rr »

If php is compiled to accept the "u" modifier you can use the Letter property code which is rendered "\p{L}":

Code: Select all

preg_match('/^[\p{L}\.\'\- ]+$/u', $subject);
The "L" property code will accept letters in any language. If you just want only English and Greek letters, you need to find the Unicode range for Greek. Perhaps [a-z\x0370-\x1FFE]? See Wikipedia's info on Unicode ranges for Greek.

Also test it on your server to ensure the "u" modifier is enabled.
Post Reply