简体   繁体   English

PHP 和 regexp 只接受希腊字符的形式

[英]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).我需要一个正则表达式,它只接受我的表单 (PHP) 中的名称字段的希腊字符和空格。 I've tried several findings on the net but no luck.我在网上尝试了几个发现,但没有运气。 Any help will be appreciated.任何帮助将不胜感激。

全字母解决方案,带重音字母:

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

The other answers here didn't work for me.这里的其他答案对我不起作用。 Greek Unicode characters are included in the following two blocks希腊语 Unicode 字符包含在以下两个块中

  • Greek and Coptic U+0370 to U+03FF (normal Greek letters)希腊语和科普特语 U+0370 到 U+03FF(普通希腊字母)
  • Greek Extended U+1F00 to U+1FFF (Greek letters with diacritics)希腊语扩展 U+1F00 到 U+1FFF(带变音符号的希腊字母)

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也可以看看

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.因此,要使用希腊语执行此操作,您需要将az替换a希腊字母表的第一个和最后一个字母。 If I remember right, those are α and ω .如果我没记错的话,那些是αω So the code would be:所以代码将是:

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

对我/^[a-zA-Z\\p{Greek}]+$/u/^[a-zA-Z\\p{Greek}]+$/u来源: http : //php.net/manual/fr/function.preg-match.php#105324

Greek & Coptic in utf-8 seem to be in the U+0370 - U+03FF range. utf-8 中的希腊语和科普特语似乎在 U+0370 - U+03FF 范围内。 Be aware: a space, a - , a .请注意:空格、 a - 、 a . etc. are not....等等不是....

To elaborate on leo pal's answer, an even more complete regex, which would accept even capital accented Greek characters, would be the following:为了详细说明 leo pal 的答案,一个更完整的正则表达式,甚至可以接受带大写重音的希腊字符,如下所示:

/^[α-ωΑ-ΩίϊΐόάέύϋΰήώΊΪΌΆΈΎΫΉΏ\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 \\s - 任何空白字符

Note: The above does not take into account ancient Greek diacritics (ᾶ, ἀ, etc.).注意:以上未考虑古希腊变音符号(ᾶ、ἀ 等)。

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!刚刚在优秀网站https://regexr.com/上注意到希腊字符的范围从“Ά”(902)到“ώ”(974),其中 3 个字符不是 aphabet 字符:“·”(903)和不可打印的字符 0907, 0909 所以范围[Ά-ώ]将覆盖 99.99% 的情况!

With (?![·\इ\उ])[Ά-ώ] covers 100%.(?![·\इ\उ])[Ά-ώ]覆盖 100%。 (I don't check this at PHP though) (虽然我没有在 PHP 上检查这个)

The modern Greek alphabet in UTF-8 is in theU+0386 - U+03CE range . UTF-8 中的现代希腊字母在U+0386 - U+03CE 范围内

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM