views:

57

answers:

2

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

e.g. 415 -> CA. 212 -> NY.

+1  A: 

As a non-American I used Google to look it up:

The easiest way would be to store the values in a database and just query it. ALternatively, you could have an array/list/... with the area codes in it and just use the languages features to retrieve the values needed.

DrColossos
I guess I could use beautifulsoup to parse that page into a clean array. I was sort of hoping someone had already done that so I don't have to.
Zachary Burt
I'm not sure DOM parsing is the easiest/best approach. You should use either a database or a list/array/... or as captaintokyo said, use a external file. You should not rely on a page because the page can go offline.
DrColossos
+1  A: 

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'
captaintokyo
I was hoping for an answer that wouldn't require me to do some HTML parsing or retyping : - )
Zachary Burt
It's two minutes work... Here you go: http://pastie.org/pastes/1242563/text
captaintokyo
this work has been done thousands times already. Google has plenty of php arrays.
Col. Shrapnel
Ah well, now Google has `plenty++` arrays...
captaintokyo
one lazy lame developer++ as well. and +25 to another repwhore.
Col. Shrapnel
@captaintokyo: What are all the area codes you have in your link that have the state set equal to '--'?
martineau
I'm not from the US, but I guess they are phone number prefixes that are not specific to a state. I got the list from DrColossos' answer: http://www.bennetyee.org/ucsd-pages/area.html
captaintokyo