Reputation: 87
I tried reading the other questions first but didn't find anything useful. I am using preg_match() function for UAE phone number validation,everything is working fine on localhost(XAMPP).But when it comes to production server am not getting the result i expect.
Php version: 7.2, Server OS: CentOS 7.4 (inmotion hosting)
Sample code for validation is given below,
$phone="971588471481";
$pattern = "/^(?:\971|00971)?(?:50|51|52|54|55|56|58|2|3|4|6|7|9)\d{7}$/";
if(preg_match($pattern, $phone)) {
echo "Match Result: <b>valid Phone Number</b>";
}else{
echo "Match Result: <b>Phone number is not valid</b>";
}
when i run the above code on my localhost the result is valid phone number. But on live server the same script gives opposite result.(Invalid Phone number). why it is giving different results? Thanks in advance.
Upvotes: 1
Views: 1016
Reputation: 627110
In PHP7, regexp efficiency is analyzed before execution, and if it is too complex from the engine point of view, execution may be halted.
You should write your patterns in a more optimized way to avoid so many alternatives that can match at the same location in the group.
You may optimize your regex the following way:
$pattern = "/^(?:(?:00)?971)?(?:5[0124568]|[234679])\d{7}$/";
See the PHP online demo.
Details
^
- start of string(?:(?:00)?971)?
- an optional sequence of
(?:00)?
- optional sequence of 00
971
- a 971
substring(?:
- either
5[0124568]
-5
followed with 0
, 1
, 2
, 4
, 5
, 6
or 8
|
- or[234679]
- 2
, 3
, 4
, 6
, 7
or 9
)
- end of the grouping\d{7}
- 7 digits$
- end of string (or \z
can be used here)/Upvotes: 1