5

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

Tech4Wilco
  • 6,740
  • 5
  • 46
  • 81
  • 8
    Who are you to validate someone's name on spelling? :) – Wesley van Opdorp Oct 25 '11 at 12:44
  • Is this really necessary? There should be no need to do more than validate i.e. the length of the name. If an user doesn't want to write his own name, he can just write anything down that'd be valid, such as "John Doe". In the contrary, your regexp might end up invalidating some more rare/exotic names. I wouldn't do this. – reko_t Oct 25 '11 at 12:45
  • 1
    Possible duplicate of [How can I find repeated letters with a Perl regex?](http://stackoverflow.com/questions/178837/how-can-i-find-repeated-letters-with-a-perl-regex) – breiti Oct 25 '11 at 12:47
  • Agreed with @WesleyvanOpdorp - see also my answer here: http://stackoverflow.com/questions/3853346/how-to-validate-human-names-in-cakephp/3853820#3853820 – Spudley Oct 25 '11 at 12:48
  • 5
    Hope you are not planning to go international. – Avada Kedavra Oct 25 '11 at 12:53
  • 4
    @WesleyvanOpdorp someone that got a project which specific requirement. Have you heard about the name: AAAAAAAAron? very popular these days – Tech4Wilco Oct 25 '11 at 12:59
  • 5
    What about my friend LLywellyn? Is he not allowed to join your site? – DaveRandom Oct 25 '11 at 12:59
  • @DaveRandom you mean LLywelyn? which is not LLywellyn – Tech4Wilco Oct 25 '11 at 13:00
  • Nope, both are valid. Google it (and ignore the `Did you mean?`). – DaveRandom Oct 25 '11 at 13:01
  • I've got a baby names book on my shelf, and out of curiosity, I decided to scan through it to see if there are any names in it that break your rules. It's taken me ten minutes to find "Saarrah". Apparently it's a derivative of "Sarah". I wouldn't name *my* child like that, but if it's common enough to be in the book, then you can bet there are at least a few people out therewith that name. I also found "Aarron" in the book. I'm not going to spend any more time looking, but I'd give good odds on there being quite a few more examples in there. – Spudley Oct 25 '11 at 13:02
  • @DaveRandom never heard but I will agree with you it's a valid name, I updated my request thanks – Tech4Wilco Oct 25 '11 at 13:02
  • @AvadaKedavra no, it's only for US – Tech4Wilco Oct 25 '11 at 13:04
  • 6
    Downvoting to -3 seems a bit harsh, this may not be the best idea in the world but it's a valid and clearly phrased question. I don't think downvoting on SO is meant for when you disagree with the idea. – Michael Low Oct 25 '11 at 13:04
  • @mikel Agreed, +1ed to counter – DaveRandom Oct 25 '11 at 13:08
  • Since [there is a guy who legally changed his name to 'PlayStation 2'](http://news.bbc.co.uk/cbbcnews/hi/uk/newsid_1844000/1844194.stm), I don't think you can apply any rules to this. Presumably you were also planning to disallow numerics? – DaveRandom Oct 25 '11 at 13:10
  • @DaveRandom I will have to allow numeric yes for David III (example) – Tech4Wilco Oct 25 '11 at 13:16

2 Answers2

14

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

RageZ
  • 26,800
  • 12
  • 67
  • 76
5

Try this regular expression:

/(\w)\1{2}/
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79