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:
- AAAron = bad because of the 3 A’s
- AAron = good
- Hannah = Good
- Hannnah = bad because of the 3 N’s
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.
the
\1means 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.
back reference is an advanced feature, luckily the PCRE functions supports it.
Note:
preg_matchwould match only one sequence, if you need to know all the matches please usepreg_match_all