/ Published in: PHP
The following codes populate the country list into dropdown box based on browser's languages. The country information is getting from geolocation.com
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php //////////////////////////////////////////////////////////////////////////////////////////////// //The following codes populate the country list into dropdown box based on browser's languages. //The country information is getting from geolocation.com, //free license with attribution: The geolocation data is provided by http://www.geolocation.com //Get the languages echo '<p>Browser Languages: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . '</p>'; echo '<p style="margin:0px; padding:0px;">Country List <span style="font-size:9pt; display:block;">The geolocation data is provided by <a href="http://www.geolocation.com">http://www.geolocation.com</a></span></p>'; //////////////////////////// //Get the browser languages if(preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_matches)){ foreach ($langs as $lang => $val) if ($val === '') $langs[$lang] = 1; } //Default to english $language = 'en-us'; foreach ($langs as $lang => $val){ $language = $lang; break; } ////////////////////// //Load the Country File //Notes: //Download the CSV file from http://www.geolocation.com //Rename the CSV to country-en-us.csv (for english version), //country-zh-cn.csv (for chinese simplified version), //country-zh-tw.csv (for chinese traditional version) //and save in the same folder as the php code resided. echo '<select>'; $alpha2_code = $matches[1][$nIdx]; $country_name = $matches[4][$nIdx]; //echo $alpha2_code . ',' . $country_name . '<br/>'; echo '<option value=' . $alpha2_code . '">' . $country_name . '</option>'; } } echo '</select>'; } else echo 'Unable to locate browser languages'; ?>
URL: http://www.geolocation.com