基于浏览器语言的国家/地区列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于浏览器语言的国家/地区列表相关的知识,希望对你有一定的参考价值。
The following codes populate the country list into dropdown box based on browser's languages. The country information is getting from geolocation.com
<?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*qs*=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'; ?>
以上是关于基于浏览器语言的国家/地区列表的主要内容,如果未能解决你的问题,请参考以下文章