PHP and regexp to accept only Greek characters in form

前端 未结 7 1964
慢半拍i
慢半拍i 2020-12-11 16:07

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

相关标签:
7条回答
  • 2020-12-11 16:33

    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.).

    0 讨论(0)
  • 2020-12-11 16:42

    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';
    
    0 讨论(0)
  • 2020-12-11 16:45

    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.

    0 讨论(0)
  • 2020-12-11 16:46

    Full letters solution, with accented letters:

    /^[A-Za-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]+$/
    
    0 讨论(0)
  • 2020-12-11 16:52

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

    0 讨论(0)
  • 2020-12-11 16:56

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

    0 讨论(0)
提交回复
热议问题