Allow only some letters, prohibit special characters ($% etc.), except others ('-)

I need Regex for PHP to do the following:

I want to allow [a-zα-ωá-źa-i ա-ֆ ა-ჰ א-ת] and Chinese, Japanese (more than utf-8) letters; I want to ban [^ 98765432100123456789] (Arabic numerals);

This is what I did:

function isValidFirstName($first_name) { return preg_match("/^(?=[a-zα-ωá-ź-ա-ֆა-ჰא-ת]+([a-zα-ωá-ź-ա-ֆა-ჰא-ת' -]+)?\z)[a-zα-ωá-ź-ա-ֆა-ჰא-ת' -]+$/i", $first_name); } 

It seems to work, but if I type letters from more than one language, it does not check.

Examples: Avpa Vapapva á-ź John - does not check. John Gger - checks, á-ź á-ź - checks.

I would like to do all this.

Or, if there is a way, an echo message if the user enters a more lingual line.

+6
source share
2 answers

I cannot reproduce cases of failures here ( á-ź John only checks the penalty), but you can simplify the regular expression - you do not need this statement:

 preg_match('/^[a-zα-ωá-ź-ա-ֆა-ჰא-ת][a-zα-ωá-ź-ա-ֆა-ჰא-ת\' -]*$/i', $first_name) 

As far as I can tell from the ranges of characters you indicated, you don't need to exclude numbers, because anything outside of these character classes will already throw a regex error.

Another consideration: if your goal is to allow any letter from any language / script (plus some punctuation and space), you can (if using Unicode strings) simplify this even further:

 preg_match('/^\pL[\pL\' -]*$/iu', $first_name) 

But actually, I wouldn’t check the name with regular expressions (or any other means): Falsehoods programmers believe in names .

+2
source

You can filter out Arabic characters by checking the following path with RegEx:

 if (preg_match('/(?:[\p{Hebrew}]+)/imu', $subject)) { # Successful match } else { # Match attempt failed } 

Description of RegEx

 <!-- (?i)(?:[\p{IsHebrew}]+) Options: case insensitive; ^ and $ match at line breaks Match the remainder of the regex with the options: case insensitive (i) «(?i)» Match the regular expression below «(?:[\p{IsHebrew}]+)» A character in the Unicode block "Hebrew" (U+0590..U+05FF) «[\p{IsHebrew}]+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» --> 
+2
source

Source: https://habr.com/ru/post/915179/


All Articles