Skip to content
Advertisement

How to convert an area code into a state using PHP or python?

I have a phone number, including area code. Is there a PHP or python-accessible API or library that will return its corresponding state?

For example:

415 -> CA
212 -> NY

Advertisement

Answer

http://www.50states.com/areacodes/

I suggest putting everything in an array in a seperate file:

// area_codes.php
$area_codes['205'] = 'AL';
$area_codes['251'] = 'AL';
$area_codes['256'] = 'AL';
$area_codes['334'] = 'AL';
$area_codes['907'] = 'AK';

// etc.

If you need the area code, just include the file and use it like this:

include('area_codes.php');
$area_code = '205';
echo $area_codes[$area_code]; // Output: 'AL'
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement