phpcountry-codesmobile-country-code

How to get the country code from mobile number using php


i am getting mobile number from database in database i am haveing more the 1000 mobile numbers,my database look like this

   mobilenumber
   971525478965
   919844005522
   45712345678

i want to go through each number in the database and find the countrycode from the mobile number display the countrycode and the country using php

for example like this

 countrycode 971 country UAE
 countrycode 91 country India
 countrycode 45 country Denmark

any one has any suggestions please guide me how to do it.

i tried like this ,but want to check more than one mobilenumber from database

 <?php

$number = "971527139011";

    $countrys = array(

    '1' => 'us',
    '2' => 'uk',
    '3' => 'de',
    '44' => 'fi',
    '123' => 'no',
    '971' =>'uae',
    '91' =>'india',
    '92' =>'pakistan'
    );

    $i = 4;
    $country = "";
    while ($i > 0) {
        if (isset($countrys[substr($number, 0, $i)])) {
            $country = $countrys[substr($number, 0, $i)];
            break;
        } else {
            $i--;
        }
    }
    echo $country;


    ?>

edited

   <?php


$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '1234fedf';
$dbDatabase = 'smsmobile';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

$sql = "SELECT destinationaddress FROM reporting";
//print $sql;

$queryRes1 = mysql_query($sql);

while($rows=mysql_fetch_assoc($queryRes1))
{
$destinationaddress[] = $rows['destinationaddress']; 

}



$phones = $destinationaddress;



// get your list of country codes
$ccodes = array(
    '1' => 'us',
    '2' => 'uk',
    '3' => 'de',
    '44' => 'fi',
    '123' => 'no',
    '971' =>'uae',
    '91' =>'india',
    '92' =>'pakistan'
);

krsort( $ccodes );

foreach( $phones as $pn )
{
    foreach( $ccodes as $key=>$value )
    {
        if ( substr( $pn, 0, strlen( $key ) ) == $key )
        {
            // match
            $country[$pn] = $value;
            break;
        }
    }
}

print_r( $country );    
    ?>

Solution

  • Something like this will work:

    // get your list of numbers from DB
    $phones = array( '971527139011', '171527139011' );
    
    // get your list of country codes
    $ccodes = array(
        '1' => 'us',
        '2' => 'uk',
        '3' => 'de',
        '44' => 'fi',
        '123' => 'no',
        '971' =>'uae',
        '91' =>'india',
        '92' =>'pakistan'
    );
    
    krsort( $ccodes );
    
    foreach( $phones as $pn )
    {
        foreach( $ccodes as $key=>$value )
        {
            if ( substr( $pn, 0, strlen( $key ) ) == $key )
            {
                // match
                $country[$pn] = $value;
                break;
            }
        }
    }
    
    print_r( $country );
    

    Edit

    The data is pretty messy, I suggest you store it in different columns, but, if the structure of the data is always the same, this will extract the country/code from it:

    $data = array( 'country : Denmark prefix:45', 'country : Pakistan prefix:92' );
    
    foreach ( $data as $string )
    {
        $_a = explode( ':', $string );
        $_b = explode( ' ', $_a[1] );
        $ccode[$_a[2]] = $_b[1];
    }
    
    print_r( $ccode );