首頁 > 後端開發 > php教程 > 过路的牛牛们,帮小菜看下这段代码吧

过路的牛牛们,帮小菜看下这段代码吧

WBOY
發布: 2016-06-23 13:38:25
原創
1041 人瀏覽過

想通过取得UA中的语言编码信息实现跳转,功能:如果UA包含非zh-CN的编码,跳转之,未设置语言信息的不跳转,
不是程序,自己查函数下午搞到现在还没实验成功,条件加循环就晕了,输出一堆no搞不定,大牛小牛帮帮忙,多谢啦!

$ua = $_SERVER['HTTP_USER_AGENT']; $lang = array("af-ZA","sq-AL","ar-DZ","ar-BH","ar-EG","ar-IQ","ar-JO","ar-KW","ar-LB","ar-LY","ar-MA","ar-OM","ar-QA","ar-SA","ar-SY","ar-TN","ar-AE","ar-YE","hy-AM","az Azeriaz-AZ-Cyrl","az-AZ-Latn Azeri","eu-ES","be-BY","bg-BG","ca-ES","zh-HK","zh-MO","zh-CN","zh-CHS","zh-SG","zh-TW","zh-CHT","hr-HR","cs-CZ","da-DK","div-MV","nl-BE","nl-NL","en-AU","en-BZ","en-CA","en-CB","en-IE","en-JM","en-NZ","en-PH","en-ZA","en-TT","en-GB","en-US","en-ZW","et-EE","fo-FO","fa-IR","fi-FI","fr-BE","fr-CA","fr-FR","fr-LU","fr-MC","fr-CH","gl-ES","ka-GE","de-AT","de-DE","de-LI","de-LU","de-CH","el-GR","gu-IN","he-IL","hi-IN","hu-HU","is-IS","id-ID","it-IT","it-CH","ja-JP","kn-IN","kk-KZ","kok-IN","ko-KR","ky-KZ","lv-LV","lt-LT","mk-MK","ms-BN","ms-MY","mr-IN","mn-MN","nb-NO","nn-NO","pl-PL","pt-BR","pt-PT","pa-IN","ro-RO","ru-RU","sa-IN","sr-SP-Cyrl","sr-SP-Latn","sk-SK","sl-SI","es-AR","es-BO","es-CL","es-CO","es-CR","es-DO","es-EC","es-SV","es-GT","es-HN","es-MX","es-NI","es-PA","es-PY","es-PE","es-PR","es-ES","es-UY","es-VE","sw-KE","sv-FI","sv-SE","syr-SY","ta-IN","tt-RU","te-IN","th-TH","tr-TR","uk-UA","ur-PK","uz-UZ-Cyrl","uz-UZ-Latn","vi-VN");for($i=0;$i<136;$i++){if(is_numeric(stripos($ua,$lang[$i])) == false){	echo "no";}else{	echo "yes";}}
登入後複製


回复讨论(解决方案)

in_array 不是更好吗?

需求是中文不跳转,非中文跳转吗?

获取客户端浏览器使用的语言应该是从$_SERVER["HTTP_ACCEPT_LANGUAGE"]中截取的。

举个栗子

if (preg_match("/zh/i", substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4)))  {    echo "不跳转";}else {    echo "跳转";}
登入後複製


语言信息在 HTTP_ACCEPT_LANGUAGE 中而不是 HTTP_USER_AGENT
HTTP_ACCEPT_LANGUAGE 实际是客户端操作系统默认字符集的反映,与用户用什么字符集浏览你的网页无关
谁告诉你说中文浏览器就不能观看纯英文的网站?

首先HTTP_ACCEPT_LANGUAGE已经提到了
其次,需求是非zh的编码跳转,你大可不必将所有的可能都列出来,用stripos判定一次即可
最后遍历数组可以试试foreach

拜谢各位!!!
继续折腾先

谢谢各位牛牛,实验了好久,
ua获取和浏览器获取是两个思路
两个都需要做,foreach很好用

很多ua里确实有语言信息,不是全部,所以还是需要判断所有

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板