How to Get State and City from USA Phone Number

It's very important if you want to detect and if you want to get location from Phone Code. If you want to get users location like state and city from phone code of USA number then this is the tutorial for you. This tutorial is built How to Get State and City from USA Phone Number let's see this. Why do you need to detect this? Because of if you want to build a tool then you can use this code. If you develop an application for a phone call and you get incoming calls and want to track number location then this code is definitely gonna help you. Let's start coding.

First of all, we are going to listed phone code and locations using two arrays. We take $codes and $cities array to list our desire cities with a phone code.

<?php
$codes = ["201","202","203","204","205","206","207","208","209","210","212","213","214","215","216","217","218","219","220","224","225","226","228","229","231","234","236","239","240","248","249","250","251","252","253","254","256","260","262","267","269","270","272","276","281","289","301","302","303","304","305","306","307","308","309","310","312","313","314","315","316","317","318","319","320","321","323","325","330","331","332","334","336","337","339","340","343","346","347","351","352","360","361","364","365","380","385","386","401","402","403","404","405","406","407","408","409","410","412","413","414","415","416","417","418","419","423","424","425","430","431","432","434","435","437","438","440","442","443","450","458","463","469","470","475","478","479","480","484","501","502","503","504","505","506","507","508","509","510","512","513","514","515","516","517","518","519","520","530","531","534","539","540","541","548","551","559","561","562","563","567","570","571","573","574","575","579","580","581","585","586","587","601","602","603","604","605","606","607","608","609","610","612","613","614","615","616","617","618","619","620","623","626","628","629","630","631","636","639","641","646","647","650","651","657","660","661","662","667","669","670","671","678","680","681","682","684","701","702","703","704","705","706","707","708","709","712","713","714","715","716","717","718","719","720","724","725","727","731","732","734","737","740","743","747","754","757","760","762","763","765","769","770","772","773","774","775","778","779","780","781","782","785","786","787","801","802","803","804","805","806","807","808","810","812","813","814","815","816","817","818","819","825","828","830","831","832","843","845","847","848","850","854","856","857","858","859","860","862","863","864","865","867","870","872","873","878","901","902","903","904","905","906","907","908","909","910","912","913","914","915","916","917","918","919","920","925","928","929","930","931","934","936","937","938","939","940","941","947","949","951","952","954","956","959","970","971","972","973","978","979","980","984","985","989"];
$cities = ["Jersey City, NJ","District of Columbia","Bridgeport, CT","Manitoba","Birmingham, AL","Seattle, WA","Portland, ME","Idaho","Stockton, CA","San Antonio, TX","New York, NY","Los Angeles, CA","Dallas, TX","Philadelphia, PA","Cleveland, OH","Springfield, IL","Duluth, MN","Hammond, IN","Newark, OH","Elgin, IL","Baton Rouge, LA","London, ON","Gulfport, MS","Albany, GA","Muskegon, MI","Akron, OH","Vancouver, BC","Cape Coral, FL","Germantown, MD","Troy, MI","Sudbury, ON","Kelowna, BC","Mobile, AL","Greenville, NC","Tacoma, WA","Killeen, TX","Huntsville, AL","Fort Wayne, IN","Kenosha, WI","Philadelphia, PA","Kalamazoo, MI","Bowling Green, KY","Scranton, PA","Bristol, VA","Houston, TX","Hamilton, ON","Germantown, MD","Delaware","Denver, CO","West Virginia","Miami, FL","Saskatchewan","Wyoming","Grand Island, NE","Peoria, IL","Los Angeles, CA","Chicago, IL","Detroit, MI","St. Louis, MO","Syracuse, NY","Wichita, KS","Indianapolis city (balance), IN","Shreveport, LA","Cedar Rapids, IA","St. Cloud, MN","Orlando, FL","Los Angeles, CA","Abilene, TX","Akron, OH","Aurora, IL","New York, NY","Montgomery, AL","Greensboro, NC","Lafayette, LA","Boston, MA","Virgin Islands","Ottawa, ON","Houston, TX","New York, NY","Lowell, MA","Gainesville, FL","Vancouver, WA","Corpus Christi, TX","Bowling Green, KY","Hamilton, ON","Columbus, OH","Salt Lake City, UT","Palm Coast, FL","Providence, RI","Omaha, NE","Calgary, AB","Atlanta, GA","Oklahoma City, OK","Montana","Orlando, FL","San Jose, CA","Beaumont, TX","Baltimore, MD","Pittsburgh, PA","Springfield, MA","Milwaukee, WI","San Francisco, CA","Toronto, ON","Springfield, MO","Quebec, QC","Toledo, OH","Chattanooga, TN","Los Angeles, CA","Bellevue, WA","Tyler, TX","Manitoba","Midland, TX","Lynchburg, VA","St. George, UT","Toronto, ON","Montreal, QC","Parma, OH","Oceanside, CA","Baltimore, MD","Granby, QC","Eugene, OR","Indianapolis city (balance), IN","Dallas, TX","Atlanta, GA","Bridgeport, CT","Macon, GA","Fort Smith, AR","Mesa, AZ","Allentown, PA","Little Rock, AR","Louisville, KY","Portland, OR","New Orleans, LA","Albuquerque, NM","New Brunswick","Rochester, MN","Worcester, MA","Spokane, WA","Oakland, CA","Austin, TX","Cincinnati, OH","Montreal, QC","Des Moines, IA","Hempstead, NY","Lansing, MI","Albany, NY","London, ON","Tucson, AZ","Redding, CA","Omaha, NE","Eau Claire, WI","Tulsa, OK","Roanoke, VA","Eugene, OR","London, ON","Jersey City, NJ","Fresno, CA","West Palm Beach, FL","Long Beach, CA","Davenport, IA","Toledo, OH","Scranton, PA","Arlington, VA","Columbia, MO","South Bend, IN","Las Cruces, NM","Granby, QC","Lawton, OK","Quebec, QC","Rochester, NY","Warren, MI","Calgary, AB","Jackson, MS","Phoenix, AZ","New Hampshire","Vancouver, BC","South Dakota","Ashland, KY","Binghamton, NY","Madison, WI","Trenton, NJ","Allentown, PA","Minneapolis, MN","Ottawa, ON","Columbus, OH","Nashville, TN","Grand Rapids, MI","Boston, MA","Belleville, IL","San Diego, CA","Hutchinson, KS","Phoenix, AZ","Pasadena, CA","San Francisco, CA","Nashville, TN","Aurora, IL","Brentwood, NY","O'Fallon, MO","Saskatchewan","Mason City, IA","New York, NY","Toronto, ON","San Mateo, CA","St. Paul, MN","Anaheim, CA","Sedalia, MO","Bakersfield, CA","Southaven, MS","Baltimore, MD","San Jose, CA","Northern Mariana Islands","Guam","Atlanta, GA","Syracuse, NY","West Virginia","Fort Worth, TX","American Samoa","North Dakota","Las Vegas, NV","Arlington, VA","Charlotte, NC","Sudbury, ON","Augusta, GA","Santa Rosa, CA","Cicero, IL","Newfoundland/Labrador","Sioux City, IA","Houston, TX","Anaheim, CA","Eau Claire, WI","Buffalo, NY","Lancaster, PA","New York, NY","Colorado Springs, CO","Denver, CO","New Castle, PA","Las Vegas, NV","St. Petersburg, FL","Jackson, TN","Toms River, NJ","Ann Arbor, MI","Austin, TX","Newark, OH","Greensboro, NC","Los Angeles, CA","Fort Lauderdale, FL","Virginia Beach, VA","Oceanside, CA","Augusta, GA","Brooklyn Park, MN","Muncie, IN","Jackson, MS","Roswell, GA","Port St. Lucie, FL","Chicago, IL","Worcester, MA","Reno, NV","Vancouver, BC","Rockford, IL","Edmonton, AB","Boston, MA","Nova Scotia/PE Island","Topeka, KS","Miami, FL","Puerto Rico","Salt Lake City, UT","Vermont","Columbia, SC","Richmond, VA","Oxnard, CA","Lubbock, TX","Kenora, ON","Hawaii","Flint, MI","Evansville, IN","Tampa, FL","Erie, PA","Rockford, IL","Kansas City, MO","Fort Worth, TX","Los Angeles, CA","Sherbrooke, QC","Calgary, AB","Asheville, NC","New Braunfels, TX","Salinas, CA","Houston, TX","Charleston, SC","New City, NY","Elgin, IL","Toms River, NJ","Tallahassee, FL","Charleston, SC","Camden, NJ","Boston, MA","San Diego, CA","Lexington-Fayette, KY","Hartford, CT","Newark, NJ","Lakeland, FL","Greenville, SC","Knoxville, TN","Northern Canada","Jonesboro, AR","Chicago, IL","Sherbrooke, QC","Pittsburgh, PA","Memphis, TN","Nova Scotia/PE Island","Tyler, TX","Jacksonville, FL","Hamilton, ON","Marquette, MI","Alaska","Elizabeth, NJ","San Bernardino, CA","Fayetteville, NC","Savannah, GA","Overland Park, KS","Yonkers, NY","El Paso, TX","Sacramento, CA","New York, NY","Tulsa, OK","Raleigh, NC","Green Bay, WI","Concord, CA","Yuma, AZ","New York, NY","Evansville, IN","Clarksville, TN","Brentwood, NY","Conroe, TX","Dayton, OH","Huntsville, AL","Puerto Rico","Denton, TX","North Port, FL","Troy, MI","Irvine, CA","Riverside, CA","Bloomington, MN","Fort Lauderdale, FL","Laredo, TX","Hartford, CT","Fort Collins, CO","Portland, OR","Dallas, TX","Newark, NJ","Lowell, MA","College Station, TX","Charlotte, NC","Raleigh, NC","Houma, LA","Saginaw, MI"]; ?>


Ok, we write our phone code in two arrays now we will replace this with a shortcode. Here is the code will be clear if you see this.


<?php
$body = 'wow you are from %%location%% my grandmother is there.';
$phone = '+14153527463';
$code = substr($phone,2,3);
$replace = str_replace($codes, $cities, $code);
$body = str_replace('{location}', $replace, $body);
echo $body;
?>


in the $body variable we stored a message for demo and we replaced the location with %%location%% shortcode. Here is a phone number +14153527463 and our system find area code from it and the area code of this phone is 415 and country code is +1. We replaced the area code with a city using str_replace() php build in function. If we run this whole code the code will execute and replace phone code to city name and will send a response with the code.


I hope you understand this, If you have any question then you can ask me without any hesitation. If you like this tutorial, please share it to encourage us to write something for you.


Sajjad Hossain
Hi, I'm Sajjad Hossain, a PHP Developer working on PHP Development since 2012. If you have a project that you want to start you can contact me. I'm currently available for freelance work. Ping me on Facebook, WhatsApp or Skype.

We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy
Top