• 梦想还是要有的,万一实现了呢...
  • 区块链论坛整装待发...

PHP自定义判断浏览器类型HTTP_USER_AGENT和国家语言HTTP_ACCEPT_LANGUAGE (附各国语言简写代码)

PHP Vetrue 7个月前 (05-07) 495次浏览 0个评论 二维码链接

判断浏览器类型:

判断浏览器语言:

判断浏览器类型实例:

<?php  
    if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 8.0"))  
    echo "Internet Explorer 8.0";  
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 7.0"))  
    echo "Internet Explorer 7.0";  
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 6.0"))  
    echo "Internet Explorer 6.0";  
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox/3"))  
    echo "Firefox 3";  
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox/2"))  
    echo "Firefox 2";  
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"Chrome"))  
    echo "Google Chrome";  
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"Safari"))  
    echo "Safari";  
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera"))  
    echo "Opera";  
    else echo $_SERVER["HTTP_USER_AGENT"];  
?>  

判断浏览器语言的具体实例:

<?php  
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //只取前 4 位,这样只判断最优先的语言。如果取前 5 位,可能出现 en,zh 的情况,影响判断。  
    if (preg_match("/zh-c/i", $lang))  
    echo "简体中文";  
    else if (preg_match("/zh/i", $lang))  
    echo "繁體中文";  
    else if (preg_match("/en/i", $lang))  
    echo "English";  
    else if (preg_match("/fr/i", $lang))  
    echo "French";  
    else if (preg_match("/de/i", $lang))  
    echo "German";  
    else if (preg_match("/jp/i", $lang))  
    echo "Japanese";  
    else if (preg_match("/ko/i", $lang))  
    echo "Korean";  
    else if (preg_match("/es/i", $lang))  
    echo "Spanish";  
    else if (preg_match("/sv/i", $lang))  
    echo "Swedish";  
    else echo $_SERVER["HTTP_ACCEPT_LANGUAGE"];  
?>  
判断浏览器类型主要是靠分析 _SERVER[“HTTP_USER_AGENT”]的内容,而分析浏览器语言则是分析 _SERVER[“HTTP_ACCEPT_LANGUAGE”]。

PHP 自定义函数判断浏览器语音:

function getPreferredLanguage() {  
    $langs = array();  
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {  
        // break up string into pieces (languages and q factors)  
        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_parse);  
        if (count($lang_parse[1])) {  
            // create a list like "en" => 0.8  
            $langs = array_combine($lang_parse[1], $lang_parse[4]);  
            // set default to 1 for any without q factor  
            foreach ($langs as $lang => $val) {  
                if ($val === '') $langs[$lang] = 1;  
            }  
            // sort list based on value  
            arsort($langs, SORT_NUMERIC);  
        }  
    }  
    //extract most important (first)  
    foreach ($langs as $lang => $val) { break; }  
    //if complex language simplify it  
    if (stristr($lang,"-")) {$tmp = explode("-",$lang); $lang = $tmp[0]; }  
    return $lang;  
}  

PHP 各国语言简写代码

Afrikaans (af)
Albanian (sq)
Basque (eu)
Bulgarian (bg)
Byelorussian (be)
Catalan (ca)
Chinese (zh)
Chinese/China (zh-cn)
Chinese/Taiwan (zh-tw)
Chinese/Hong Kong (zh-hk)
Chinese/singapore (zh-sg)
Croatian (hr)
Czech (cs)
Danish (da)
Dutch (nl)
Dutch/Belgium (nl-be)
English (en)
English/United Kingdom (en-gb)
English/United Satates (en-us)
English/Australian (en-au)
English/Canada (en-ca)
English/New Zealand (en-nz)
English/Ireland (en-ie)
English/South Africa (en-za)
English/Jamaica (en-jm)
English/Belize (en-bz)
English/Trinidad (en-tt)
Estonian (et)
Faeroese (fo)
Farsi (fa)
Finnish (fi)
French (fr)
French/Belgium (fr-be)
French/France (fr-fr)
French/Switzerland (fr-ch)
French/Canada (fr-ca)
French/Luxembourg (fr-lu)
Gaelic (gd)
Galician (gl)
German (de)
German/Austria (de-at)
German/Germany (de-de)
German/Switzerland (de-ch)
German/Luxembourg (de-lu)
German/Liechtenstein (de-li)
Greek (el)
Hindi (hi)
Hungarian (hu)
Icelandic (is)
Indonesian (id or in)
Irish (ga)
Italian (it)
Italian/ Switzerland (it-ch)
Japanese (ja)
Korean (ko)
Latvian (lv)
Lithuanian (lt)
Macedonian (mk)
Malaysian (ms)
Maltese (mt)
Norwegian (no)
Polish (pl)
Portuguese (pt)
Portuguese/Brazil (pt-br)
Rhaeto-Romanic (rm)
Romanian (ro)
Romanian/Moldavia (ro-mo)
Russian (ru)
Russian /Moldavia (ru-mo)
Scots Gaelic (gd)
Serbian (sr)
Slovack (sk)
Slovenian (sl)
Sorbian (sb)
Spanish (es or es-do)
Spanish/Argentina (es-ar)
Spanish/Colombia (es-co)
Spanish/Mexico (es-mx)
Spanish/Spain (es-es)
Spanish/Guatemala (es-gt)
Spanish/Costa Rica (es-cr)
Spanish/Panama (es-pa)
Spanish/Venezuela (es-ve)
Spanish/Peru (es-pe)
Spanish/Ecuador (es-ec)
Spanish/Chile (es-cl)
Spanish/Uruguay (es-uy)
Spanish/Paraguay (es-py)
Spanish/Bolivia (es-bo)
Spanish/El salvador (es-sv)
Spanish/Honduras (es-hn)
Spanish/Nicaragua (es-ni)
Spanish/Puerto Rico (es-pr)
Sutu (sx)
Swedish (sv)
Swedish/Findland (sv-fi)
Thai (ts)
Tswana (tn)
Turkish (tr)
Ukrainian (uk)
Urdu (ur)
Vietnamese (vi)
Xshosa (xh)
Yiddish (ji)
Zulu (zu)

Vetrue , 版权所有丨如未注明 , 均为原创丨转载请注明原文链接
原文标题:PHP 自定义判断浏览器类型 HTTP_USER_AGENT 和国家语言 HTTP_ACCEPT_LANGUAGE (附各国语言简写代码)
原文链接:https://vetrue.com/article/280.html
喜欢 (1)
发表我的评论
取消评论
表情 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址