I would like to detect whether a user-chosen pin contains 4 identical numbers e.g 1111 or 2222. I'm using preg_match in PHP.
How can I adapt this answer to do this?
I would like to detect whether a user-chosen pin contains 4 identical numbers e.g 1111 or 2222. I'm using preg_match in PHP.
How can I adapt this answer to do this?
You could use this regex:
/(\d)\1{3}/
This matches a single digit (\d)
, and then matches that same digit 3 times \1{3}
.
count(array_unique(str_split($pin))) > 1
Adapting from the answer you link to:
\b(\d)\1{3}\b
Instead of using \1+
that would match any number of repetitions of the first digit, you substitute it with \1{3}
that will only allow three repetitions of the first digit, thus giving you the desired four digits when matched.
Or if you prefer:
\b(\d)\1\1\1\b