Php get all countries

how do i get a list of countries in my website

I am trying to find a way to display all countries list in a dropdown. Is there any short code in php or jquery to get a list of countries on my webpage? Thank you in advance.

5 Answers 5

Browsers that support Intl can now offer you the countries in the language of your choice.

function getCountries(lang = 'en') < const A = 65 const Z = 90 const countryName = new Intl.DisplayNames([lang], < type: 'region' >); const countries = <> for(let i=A; i > > return countries > 

Calling getCountries() will return an object list of country codes with their names eg.

This is awesome. For context to others looking here, the solution loops over all possible combinations of two characters ( AA, AB, AC, … ) and checks if Intl knows it as a valid country code.

If you also want a list of names mapped to country codes, you can do Object.fromEntries(Object.entries(COUNTRY_CODES_TO_NAMES).map(entry => [entry[1], entry[0]])) where COUNTRY_CODES_TO_NAMES is the output of @Gerardlamo ‘s function.

Too bad, codes can be wrong because FF & Chrome retained previously used ISO 3166-1 alpha-2 codes for various countries (Antarctica, Panama, . ), so if building Array of Arrays you get double & triple entries, or if building an Object, as in this answer, the last lexical code wins. This answer might still be useful for just names (without relying on the codes), or to generate a starting point literal string.

Читайте также:  Window functions in python

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

🌍 PHP Countries is a library that provides an elegant syntax to country data.

License

rapidwebltd/php-countries

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

PHP Countries is a library that provides an elegant syntax to country data.

You can install PHP Countries via Composer, as follows.

composer require rapidwebltd/php-countries 

To use PHP Countries, you must create a new Countries object.

use RapidWeb\Countries\Countries; $countries = new Countries;

You can then call various methods on this object, to get country data.

You can easily retrieve an array of all countries and iterate through them, as follows.

foreach($countries->all() as $country) < var_dump($country->name.' - '.$country->officialName); >

Retrieving country by name

Читайте также:  Choose python version ubuntu

Country details can be retrieved from the country’s official or common name.

var_dump($countries->getByName('United Kingdom')); /* object(RapidWeb\Countries\Country)#4869 (16)  ["name"]=> string(14) "United Kingdom" ["officialName"]=> string(52) "United Kingdom of Great Britain and Northern Ireland" ["topLevelDomains"]=> array(1)  [0]=> string(3) ".uk" > ["isoCodeAlpha2"]=> string(2) "GB" ["isoCodeAlpha3"]=> string(3) "GBR" ["isoCodeNumeric"]=> string(3) "826" ["languages"]=> array(1)  [0]=> string(7) "English" > ["languageCodes"]=> array(1)  [0]=> string(3) "eng" > ["currencyCodes"]=> array(1)  [0]=> string(3) "GBP" > ["callingCodes"]=> array(1)  [0]=> string(2) "44" > ["capital"]=> string(6) "London" ["region"]=> string(6) "Europe" ["subregion"]=> string(15) "Northern Europe" ["latitude"]=> int(54) ["longitude"]=> int(-2) ["areaInKilometres"]=> int(242900) > */

Retrieving country by ISO 3166-1 code

You can get the data for a country by its ISO 3166-1 code. The 2 character, 3 character and numeric variations are all accepted.

var_dump($countries->getByIsoCode('USA')); /* object(RapidWeb\Countries\Country)#4693 (16)  ["name"]=> string(13) "United States" ["officialName"]=> string(24) "United States of America" // etc. > */

Retrieving country by language spoken

Providing a language, will return an array of all countries in which that language is spoken. You can provide a language name or code.

var_dump($countries->getByLanguage('German')); /* array(5)  [0]=> object(RapidWeb\Countries\Country)#4913 (16)  ["name"]=> string(7) "Belgium" ["officialName"]=> // etc. > [1]=> object(RapidWeb\Countries\Country)#4883 (16)  ["name"]=> string(7) "Germany" ["officialName"]=> string(27) "Federal Republic of Germany" // etc. > [2]=> object(RapidWeb\Countries\Country)#4826 (16)  ["name"]=> string(13) "Liechtenstein" ["officialName"]=> string(29) "Principality of Liechtenstein" // etc. > [3]=> object(RapidWeb\Countries\Country)#4808 (16)  ["name"]=> string(10) "Luxembourg" ["officialName"]=> string(25) "Grand Duchy of Luxembourg" // etc. > [4]=> object(RapidWeb\Countries\Country)#4871 (16)  ["name"]=> string(7) "Namibia" ["officialName"]=> string(19) "Republic of Namibia" // etc. > > */

About

🌍 PHP Countries is a library that provides an elegant syntax to country data.

Источник

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

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. 

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!

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

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.

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..

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.

Источник

Оцените статью