Heim > Backend-Entwicklung > PHP-Tutorial > 过路的牛牛们,帮小菜看下这段代码吧

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

WBOY
Freigeben: 2016-06-23 13:38:25
Original
1040 Leute haben es durchsucht

想通过取得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";}}
Nach dem Login kopieren


回复讨论(解决方案)

in_array 不是更好吗?

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

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

举个栗子

if (preg_match("/zh/i", substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4)))  {    echo "不跳转";}else {    echo "跳转";}
Nach dem Login kopieren


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

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

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

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

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

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage