How to get a phone number to a single type with an extension number?

Hello. There is a need to give a phone number to a common view, but some of them are absolutely in the arbitrary form with additional rooms.
Now I have implemented the cast only numbers (no extension) - /[^0-9]/ . But what if the person is for example, introduced plus, I tried to do this:
1) select all the numbers in the string
2) select all the letters in string
3) divide the value of the phone found on line
Total "7(111)111-11-11 EXT. 16" -> 71111111111 EXT. 16 (then glue as I have).
Now the question is, if the person made a mistake and will introduce 7111й111111, all down the drain, there is some way to get around that? Simply search algorithm all the letters and then dividing by the substring on these letters(all) is not very reliable it seems...
July 2nd 19 at 18:17
1 answer
July 2nd 19 at 18:19
Main number and extension I would partition into 2 different fields is the best option. And of course would keep them separately in the database.

First I formatted the phone number, and then have added your extension if it is

Leave phone number only numbers, and format in the view (the function itself is simple, for example)
echo format_phone('7wer9e85w11-1"!1н1*11'); // returns: +7 (985) 111-11-11

function format_phone($phone = ")
{
 $phone = preg_replace('/[^0-9]/', ", $phone); // returns 79851111111

 if (strlen($phone) != 11 and ($phone[0] != '7' or $phone[0] != '8')) {
 return FALSE;
}

 $phone_number['dialcode'] = substr($phone, 0, 1);
 $phone_number['code'] = substr($phone, 1, 3);
 $phone_number['phone'] = substr($phone, -7);
 $phone_number['phone_arr'][] = substr($phone_number['phone'], 0, 3);
 $phone_number['phone_arr'][] = substr($phone_number['phone'], 3, 2);
 $phone_number['phone_arr'][] = substr($phone_number['phone'], 5, 2); 

 $format_phone = '+' . $phone_number['dialcode'] . '('. $phone_number['code'] .') '. implode('-', $phone_number['phone_arr']);

 return $format_phone;
 }

Find more questions by tags Regular expressionsPHP