Convert mobile number to international format dynamically with php

I often need to covert mobile numbers into a UK international format of 44 followed by the number such as 442548951486 the numbers I work with can be in a mixed format some without a starting 0 others with 44 and same with spaces, so I wrote function that will convert a number in any mixture into a standard format quick and easily and as all the code is inside a function is reusable which means converting numbers in bulk is now very easy to do.

The function simple needs a number passing to it, a check is done to make sure the number only contains numbers then a check is done to see if the number starts with a 0 if not if will add a 0 as long as the start is not 4. Then a check is done to replace the 0 with a 44 then finally any spaces are removed and the formatted number is then returned.

Here’s the function:

Usage Example: