php准确获取浏览器默认语言

准确获取用户浏览器语言,不用截取判断

//获取浏览器默认语言
function getDefaultLanguage() {
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
return parseDefaultLanguage($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
else
return parseDefaultLanguage(NULL);
}
//识别
function parseDefaultLanguage($http_accept, $deflang = "zh") {
if(isset($http_accept) && strlen($http_accept) > 1) {
$x = explode(",",$http_accept);
foreach ($x as $val) {
if(preg_match("/(.*);q=([0-1]{0,1}.\d{0,4})/i",$val,$matches))
$lang[$matches[1]] = (float)$matches[2];
else
$lang[$val] = 1.0;
}
$qval = 0.0;
foreach ($lang as $key => $value) {
if ($value > $qval) {
$qval = (float)$value;
$deflang = $key;
}
}
}
return strtolower($deflang);
}