How to list all countries and its states/provinces in php? [closed]
Asked Answered
O

6

6

Is there any quick guide/reference to list all the countries and its states/provinces?

Like when I click US, it will list: Alabama, alaska etc

Osteo answered 15/9, 2009 at 3:38 Comment(0)
P
3

I don't have code to do it, but if you want a good (free) list of administrative districts for most of the world you can use this Wikipedia page.

Prompter answered 15/9, 2009 at 3:47 Comment(2)
Should I copy the list from others website? Is it consider stealing? I know, I should not do that, but, I am stuck in listing countries..Osteo
copying the information from Wikipedia is not stealing. As stated in the Wikipedia TOS: "You can re-use content from Wikimedia projects freely, with the exception of content that is used under "fair use" exemptions, or similar exemptions of copyright law" (see: wikimediafoundation.org/wiki/Terms_of_Use for more info) As for randy melder's answer, the user is posting it for others to use and it was generated from ISO data which is freely distributed for all to use.Prompter
A
8

pycountry is the most complete free reference I was ever able to find:

pycountry provides the ISO databases for the standards:

639 Languages
3166 Countries
3166-2 Subdivisions of countries
4217 Currencies
15924 Scripts

The databases are imported from Debian's pkg-isocodes, packaged into pycountry and made accessible through a Python API.

Translation files for the various strings are included as well.
Alishiaalisia answered 15/9, 2009 at 13:13 Comment(1)
You wouldn't believe how long I looked for something like this. It contains a XML file with all the different first and sometimes second level subdivisions of countries, which is just what I needed. Everyone else just says to use Geonames which doesn't contain this information. Thanks!Speaks
F
4

Grabbing data from Geonames would be an option. It is kept relatively up to date, and is under a CC license.

Fitts answered 15/9, 2009 at 13:18 Comment(1)
This Geonames file contains all of the subdivisions for each country code. It is tab-separated.Jakie
P
3

I don't have code to do it, but if you want a good (free) list of administrative districts for most of the world you can use this Wikipedia page.

Prompter answered 15/9, 2009 at 3:47 Comment(2)
Should I copy the list from others website? Is it consider stealing? I know, I should not do that, but, I am stuck in listing countries..Osteo
copying the information from Wikipedia is not stealing. As stated in the Wikipedia TOS: "You can re-use content from Wikimedia projects freely, with the exception of content that is used under "fair use" exemptions, or similar exemptions of copyright law" (see: wikimediafoundation.org/wiki/Terms_of_Use for more info) As for randy melder's answer, the user is posting it for others to use and it was generated from ISO data which is freely distributed for all to use.Prompter
I
2

As Squeeks says you can use GeoNames.

I did a tool ( https://github.com/yosoyadri/GeoNames-XML-Builder ) that downloads and saves GeoNames response as a XML file based on the library at http://geonamesdotorgdotnet.codeplex.com/.

You can also download a complete file of countries and states/provinces at https://raw.github.com/yosoyadri/GeoNames-XML-Builder/master/continents-countries-statesprovinces.xml

Icosahedron answered 4/3, 2012 at 13:30 Comment(1)
It's a bit messy tho, at least for my country. The <Id>2742026</Id> is also a district like the rest (so either one of them is not consistent). Plus, there are only 2 entries for the local name, one of them is in English and the other one in Spanish, but <Id>2735941</Id> only lists one (Spanish) and the it's English in reality. The <Id>2267056</Id> only lists the Portuguese name under the LocalNameES tag.Alishiaalisia
R
1

In case you don't want to use sql, here is just a javascript with region country's province/state, i found it a while ago. Maybe its of good use....

http://www.sitepoint.com/forums/showthread.php?350291-Country-State-City-Dropdown-list

Riha answered 14/10, 2011 at 21:35 Comment(1)
city_states['Portugal'] = '|Lisbon||Aveiro|Acores (Azores)|Beja|Braga|Braganca|Castelo Branco|Coimbra|Evora|Faro|Guarda|Leiria|Lisboa|Madeira|Portalegre|Porto|Santarem|Setubal|Viana do Castelo|Vila Real|Viseu'; has Lisbon repeated twice - doesn't seem very reliable.Alishiaalisia
H
-1

This is as close as it gets. http://27.org/isocountrylist/

This list is rather old, however it does offer a great deal of value for those wanting a country list, with codes and abbreviations. Also, there is a link to US States and provinces there too. This has been a good resource over the years.

With this table data, it's possible for someone to create some normalized data between countries, states, provinces, counties or whatever political jurisdictions.

I thought this would be a huge time saver for this task.

Hunfredo answered 15/9, 2009 at 3:44 Comment(5)
um.. the last update is 2006. Should I worry about that?Osteo
@Osteo - Not too much has changed since 2006. You could cross reference this list with the Wikipedia page to make sure it is up to date.Evangelicalism
@christ: Thanks. Listing the countries, states/provinces, valid ZIP code, valid phone numbers are really big pain is ***...Osteo
@Christian: What about Kosovo then?Alishiaalisia
@kanayaki: Yeah https://mcmap.net/q/754772/-i18n-validations wanna help?Alishiaalisia

© 2022 - 2024 — McMap. All rights reserved.