I have number sent from form like this:
0641234567
064-123/4567
064/123-4567
3816412345678
And it needs to be like this:
+3816412345678
with +, without 0 and with max 14 characters including '+'.
How can i solve that using regex?
I have number sent from form like this:
0641234567
064-123/4567
064/123-4567
3816412345678
And it needs to be like this:
+3816412345678
with +, without 0 and with max 14 characters including '+'.
How can i solve that using regex?
All you need is multiple replacement with basically three rules,
-
or /
with empty string+381
+
in the beginning of number if the first number is any 1 to 9Check this PHP Demo,
$arr = ['0641234567','064-123/4567','064/123-4567','3816412345678'];
foreach($arr as $s) {
echo $s." --> ".preg_replace(['/^0/', '/^(?=[1-9])/', '/[-\/]/'], ['+381', '+', ''], $s)."
";
}
Prints,
0641234567 --> +381641234567
064-123/4567 --> +381641234567
064/123-4567 --> +381641234567
3816412345678 --> +3816412345678
Let me know if any of your case goes uncovered.