Catch Duplicate Letters in PHP - Regular Expressions

I need to check the user input to make sure that the first name, last name (etc.) are entered correctly and are valid. I have to create a regular expression that checks if the user enters duplicate letters in the first name, last name, etc.

Example:

  • AAAron = poor due to 3 A
  • AAron = good
  • Hannah = Good
  • Hannnah = bad due to 3 N

Is there a PHP regular expression to catch these cases? (I have a basic knowledge of regular expression, but for me it is too much)

EDIT: It should also contain the numbers: David 3 or III

thank

+5
source share
2 answers

.

preg_match('/(\w)(\1+)/', $subject, $matches);
print_r($matches);

\1 , \w.

, , - ?

.

$charCountArray = array();
foreach ($name as $char) {
    $charCountArray[$char]++;
}

- , , PCRE .

: preg_match , , preg_match_all

+14

:

/(\w)\1{2}/
+5

All Articles