I’m trying to port this java to php:
String _value = '1111122222';
if (_value.matches("(1{5}|2{5}|3{5}|4{5}|5{5}|6{5}|7{5}|8{5}|9{5}){2}")) {
// check for number with the same first 5 and last 5 digits
return true;
}
As the comment suggests, I want to test for a string like ‘1111122222’ or ‘5555566666’
How can I do this in PHP?
Thanks,
Scott
You can use
preg_matchto do so:This returns the number of matches (i.e. either 0 or 1) or false if there was an error. Since the String’s
matchesmethod returns only true if the whole string matches the given pattern butpreg_matchdoesn’t (a substring suffices), you need to set markers for the start and the end of the string with^and$.You can also use this shorter regular expression:
And if the second sequence of numbers needs to be different from the former, use this: