title: How to get the country code from mobile number using php
body: <p>i am getting mobile number from database in database i am haveing more the 1000 mobile numbers,my database look like this</p>
  
  <pre><code>   mobilenumber
     971525478965
     919844005522
     45712345678
  </code></pre>
  
  <p>i want to go through each number in the database and find the <code>countrycode</code> from the mobile number display the <code>countrycode</code> and the country using php</p>
  
  <p>for example  like this</p>
  
  <pre><code> countrycode 971 country UAE
   countrycode 91 country India
   countrycode 45 country Denmark
  </code></pre>
  
  <p>any one has any suggestions please guide me how to do it.</p>
  
  <p>i tried like this ,but want to check more than one mobilenumber from database </p>
  
  <pre><code> &lt;?php
  
  $number = "971527139011";
  
      $countrys = array(
  
      '1' =&gt; 'us',
      '2' =&gt; 'uk',
      '3' =&gt; 'de',
      '44' =&gt; 'fi',
      '123' =&gt; 'no',
      '971' =&gt;'uae',
      '91' =&gt;'india',
      '92' =&gt;'pakistan'
      );
  
      $i = 4;
      $country = "";
      while ($i &gt; 0) {
          if (isset($countrys[substr($number, 0, $i)])) {
              $country = $countrys[substr($number, 0, $i)];
              break;
          } else {
              $i--;
          }
      }
      echo $country;
  
  
      ?&gt;
  </code></pre>
  
  <p>edited</p>
  
  <pre><code>   &lt;?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' =&gt; 'us',
      '2' =&gt; 'uk',
      '3' =&gt; 'de',
      '44' =&gt; 'fi',
      '123' =&gt; 'no',
      '971' =&gt;'uae',
      '91' =&gt;'india',
      '92' =&gt;'pakistan'
  );
  
  krsort( $ccodes );
  
  foreach( $phones as $pn )
  {
      foreach( $ccodes as $key=&gt;$value )
      {
          if ( substr( $pn, 0, strlen( $key ) ) == $key )
          {
              // match
              $country[$pn] = $value;
              break;
          }
      }
  }
  
  print_r( $country );    
      ?&gt;
  </code></pre>