title: Determine Country from Telephone Numbers
body: <p>I have seen a few question on SO similar to what I require but nothing seems to fit the bill.</p>
  
  <p>I am in the position where I need to deal with a call record and determine the country using the phone number. The number dialed can be any country for example:</p>
  
  <p>44 7899455120 - UK</p>
  
  <p>34 965791845 - Spain</p>
  
  <p>355 788415235 - Albania</p>
  
  <p>Obviously the world would be great if all calling codes were two digits but this is not the case. Currently I have a database full of countries and their relevant codes and in order to match I need to effectively take the first digit of the number ie <code>4</code> for the UK example and do a query of the database eg:</p>
  
  <pre><code>SELECT * from countries WHERE code LIKE '4%'
  </code></pre>
  
  <p>This may give me for example 20 results. So I loop again and do say</p>
  
  <pre><code>SELECT * from countries WHERE code LIKE '44%'
  </code></pre>
  
  <p>This may give me say one result, now I can determine it is UK. Some codes however like Albania are three digits and require more loops and database calls. This seems quite rudimentary and inefficient but as is I cannot think of another way to achieve this. I realise three calls to a database may not seem like much but if you have 1000 calls to deal with they soon add up.</p>
  
  <p>Looking at the following question:</p>
  
  <p><a href="https://stackoverflow.com/questions/2113908/what-regular-expression-will-match-valid-international-phone-numbers">What regular expression will match valid international phone numbers?</a></p>
  
  <p>There seems to be some great information on validating a number against country codes, but not so much on determining the country code from a number. Any advice or suggestions on a cleaner method would be much appreciated.</p>
  
  <hr>
  
  <p>Spaces in the phone are shown for clarity</p>