3

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 );    
    ?>
Xavi
  • 2,552
  • 7
  • 40
  • 59
  • Take the first 3 numbers and check if they match a country code. If not, check the first two. If still no match, check only the first? – Peon May 06 '14 at 07:13
  • i did but i want to go through the numbers in database can i use while loop... – Xavi May 06 '14 at 07:14
  • You can get a full list of country codes and phone numbers from http://country.io/data if you don't have one already – Ben Dowling Sep 05 '14 at 02:53

1 Answers1

12

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 );
Peon
  • 7,902
  • 7
  • 59
  • 100
  • it is working fine but i want to take all mobile number from database ans check can u guide me – Xavi May 06 '14 at 07:31
  • thanks i am trying to get data from database please see my updated code its not working – Xavi May 06 '14 at 07:42
  • here is $ccodes is defined manually in an array but i am have in database like country and prefix i want to take that country and prefix and put inside the $ccodes can you help me,thanks for the help – Xavi May 06 '14 at 07:52
  • example: of database country : Denmark prefix:45 i want it take it from database and put it in side of $ccodes and compare with the number – Xavi May 06 '14 at 07:54
  • `$destinationaddress[] = $rows['destinationaddress'];` whats the content of this array once you fill it? – Peon May 06 '14 at 07:56
  • please see my code its working fine fine but i want to take cccode from database the database contain 'country : Denmark prefix:45' – Xavi May 06 '14 at 08:00
  • Edited, but please, take some effort on your own to figure this one out. It's not that hard. – Peon May 06 '14 at 08:10
  • if you there i need another help from you $string = 'country : Pakistan prefix:92'; but i want display the values from database in the string i have more that 100 values in database can you you guide how to put all values in string . – Xavi May 06 '14 at 08:48
  • Just add another `foreach`. – Peon May 06 '14 at 09:08