bikey77
bikey77

Reputation: 6672

PHP and regexp to accept only Greek characters in form

I need a regular expression that accepts only Greek chars and spaces for a name field in my form (PHP). I've tried several findings on the net but no luck. Any help will be appreciated.

Upvotes: 16

Views: 18943

Answers (8)

Suragch
Suragch

Reputation: 511866

The other answers here didn't work for me. Greek Unicode characters are included in the following two blocks

  • Greek and Coptic U+0370 to U+03FF (normal Greek letters)
  • Greek Extended U+1F00 to U+1FFF (Greek letters with diacritics)

The following regex matches whole Greek words:

[\u0370-\u03ff\u1f00-\u1fff]+

I will let the reader translate that to whichever programming language format they may be using.

See also

Upvotes: 13

Just noticed at the excellent site https://regexr.com/ that the range of Greek characters are from "Ά" (902) to "ώ" (974) with 3 characters that are not aphabet characters: "·" (903) and unprintable characters 0907, 0909 So a range [Ά-ώ] will cover 99.99% of the cases!

With (?![·\u0907\u0909])[Ά-ώ] covers 100%. (I don't check this at PHP though)

Upvotes: 1

Bill Tsagkas
Bill Tsagkas

Reputation: 539

To elaborate on leo pal's answer, an even more complete regex, which would accept even capital accented Greek characters, would be the following:

/^[α-ωΑ-ΩίϊΐόάέύϋΰήώΊΪΌΆΈΎΫΉΏ\s]+$/

With this, you get:

  • α-ω - lowercase letters
  • Α-Ω - uppercase letters
  • ίϊΐόάέύϋΰήώ - lowercase letters with all (modern) diacritics
  • ΊΪΌΆΈΎΫΉΏ - uppercase letters with all (modern) diacritics
  • \s - any whitespace character

Note: The above does not take into account ancient Greek diacritics (ᾶ, ἀ, etc.).

Upvotes: 4

Justin Morgan
Justin Morgan

Reputation: 30760

I'm not too current on the Greek alphabet, but if you wanted to do this with the Roman alphabet, you would do this:

/^[a-zA-Z\s]*$/

So to do this with Greek, you replace a and z with the first and last letters of the Greek alphabet. If I remember right, those are α and ω. So the code would be:

/^[α-ωΑ-Ω\s]*$/

Upvotes: 14

D3v
D3v

Reputation: 345

What worked for me was /^[a-zA-Z\p{Greek}]+$/u source: http://php.net/manual/fr/function.preg-match.php#105324

Upvotes: 2

leo pal
leo pal

Reputation: 321

Full letters solution, with accented letters:

/^[A-Za-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]+$/

Upvotes: 32

goten002
goten002

Reputation: 43

The modern Greek alphabet in UTF-8 is in the U+0386 - U+03CE range.

So the regex you need to accept Greek only characters is:

$regex_gr = '/^[\x{0386}-\x{03CE}]+$/u';

or (with spaces)

$regex_gr_with_spaces = '/^[\x{0386}-\x{03CE}\s]+$/u';

Upvotes: -1

Wrikken
Wrikken

Reputation: 70490

Greek & Coptic in utf-8 seem to be in the U+0370 - U+03FF range. Be aware: a space, a -, a . etc. are not....

Upvotes: 0

Related Questions