PHP识别浏览器的类型以及版本

因为各个浏览器具有不同的内核,对于页面处理渲染的方式也会不一样,所以经常会遇到浏览器兼容性问题,于是针对不同浏览器需要做一些不同的处理方式,第一步必然就是判断浏览器类型以及他的版本问题了。
除了使用js脚本、css区分浏览器版本外,php本身也可以识别各种不同的浏览器以及版本,本文将实现PHP识别浏览器的类型以及版本。
mozilla/5.0 applewebkit/537.36 (khtml, like gecko; compatible; claudebot/1.0; +claudebot@anthropic.com)
实现代码:
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if($agent){
 $broswer = ''; $version = '';
 if(strpos($agent,"chrome")){
 $version = stristr($agent,"chrome");
 $version = stristr($version,"safari",true);
 $version = substr($version,7);
 $broswer = '谷歌Chrome';
 }else if(strpos($agent,"firefox")){
 $version = strrchr($agent,"/");
 $version = substr($version,1);
 $broswer = '火狐Firefox';
 }else if(strpos($agent,"trident")){
 $version = stristr($agent,"trident");
 if(strpos($agent,"rv:11.0")){
 $broswer = '微软IE11';
 $version = stristr($version,";",true);
 }else if(strpos($agent,"msie 10.0")){
 $broswer = '微软IE10';
 $version = stristr($version,")",true);
 }else if(strpos($agent,"msie 9.0")){
 $broswer = '微软IE9';
 $version = stristr($version,")",true);
 }else if(strpos($agent,"msie 8.0")){
 $broswer = '微软IE8';
 $version = stristr($version,")",true);
 }
 }
 if($broswer && $version){
 echo '你的浏览器为'.$broswer.'浏览器,版本为:'.$version;
 }else{
 echo $agent;
 }
}else{
 echo 'unknown your browser !!!';
}