PHP:如果Internet Explorer 6、7、8或9

我想在PHP中针对Internet Explorer的不同版本执行以下条件:


if($browser == ie6){ //do this} elseif($browser == ie7) { //dothis } elseif...


我在相似的代码上看到了许多变体,但是正在寻找一种超级简单的东西,非常容易编写一些if和else并做不同的事情。


谢谢


编辑:我需要它来向用户显示一些不同的消息,所以CSS条件等都是不好的。


守着星空守着你
浏览 499回答 3
3回答

函数式编程

这是我最终使用的变体,它检查IE8及以下版本:if (preg_match('/MSIE\s(?P<v>\d+)/i', @$_SERVER['HTTP_USER_AGENT'], $B) && $B['v'] <= 8) {&nbsp; &nbsp; // Browsers IE 8 and below} else {&nbsp; &nbsp; // All other browsers}

慕虎7371278

可以继续与IE10和IE11一起使用的版本:preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches);if(count($matches)<2){&nbsp; preg_match('/Trident\/\d{1,2}.\d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches);}if (count($matches)>1){&nbsp; //Then we're using IE&nbsp; $version = $matches[1];&nbsp; switch(true){&nbsp; &nbsp; case ($version<=8):&nbsp; &nbsp; &nbsp; //IE 8 or under!&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case ($version==9 || $version==10):&nbsp; &nbsp; &nbsp; //IE9 & IE10!&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case ($version==11):&nbsp; &nbsp; &nbsp; //Version 11!&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; //You get the idea&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP