Note that there are some explanatory texts on larger screens.

plurals
  1. POformatting local mobile to international in php
    primarykey
    data
    text
    <p>I am looking for a simple regex to convert both UK (44) and Indian (91) numbers into a valid international format using PHP. The formats required are:</p> <pre><code>447856555333 (for uk mobile numbers) 919876543456 (for indian mobile numbers) </code></pre> <p>I need a regex that will accept and format the following variations:</p> <pre><code>1) 07856555333 2) 0785 6555333 3) 0785 655 5333 4) 0785-655-5333 5) 00447856555333 6) 0044785 6555333 7) 0044785 655 5333 8) 0044785-655-5333 9) 00447856555333 10) +447856555333 11) +44785 6555333 12) +44785 655 5333 13) +44785-655-5333 14) +919876543456 15) 00919876543456 </code></pre> <p>Any help would be much appreciated. </p> <p>UPDATE: Based on answer below I have amended the code slightly and it works very well. It is not bullet proof but covers most of the popular formats:</p> <pre><code> public static function formatMobile($mobile) { $locale = '44'; //need to update this $sms_country_codes = Config::get('sms_country_codes'); //lose any non numeric characters $numeric_p_number = preg_replace("#[^0-9]+#", "", $mobile); //remove leading zeros $numeric_p_number = preg_replace("#^[0]*#", "", $numeric_p_number); //get first 2 digits $f2digit = substr($numeric_p_number, 0,2); if(strlen($numeric_p_number) == 12) { if(in_array($f2digit, $sms_country_codes) ) { //no looks ok } else { return ""; //is correct length but missing country code so must be invalid! } } else { if(strlen($locale . $numeric_p_number) == 12 &amp;&amp; !(in_array($f2digit, $sms_country_codes))) { $numeric_p_number = $locale . $numeric_p_number; //the number is ok after adding the country prefix } else { //something is missing from here return ""; } } return $numeric_p_number; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload