I have to check user input to make sure name, last name (etc...) are entered correctly and are valid. I have to build a regexp that check if a user enters repeated letters in the first name , last name etc...
Example:
Is there a PHP regular expression to catch these cases? (I have a basic regexp knowledge but this is too much for me)
EDIT: This should Allow numbers as well: David 3 or III
Thanks
You can use back reference for that purpose.
preg_match('/(\w)(\1+)/', $subject, $matches);
print_r($matches);
the \1
means repeat the first capture so in that case \w
.
In the case of your example, I don't think using regular expression would be the best solution, why don't you just count the number of instance of any characters?
i.e.
$charCountArray = array();
foreach ($name as $char) {
$charCountArray[$char]++;
}
back reference is an advanced feature, luckily the PCRE functions supports it.
Note: preg_match
would match only one sequence, if you need to know all the matches please use preg_match_all
Try this regular expression:
/(\w)\1{2}/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With