使用 Mobile Detect 直接获取操作系统和设备名称

我前面介绍了 Mobile Detect 这个轻量级的开源移动设备(手机和平板)检测的 PHP 类库,这个类库主要功能检测是否为输入的设备,比如 $detect->isiPhone() 就是检测当前设备是否为 iPhone,但是如果要做移动统计的话,怎样不用判断直接获取操作系统和设备名称呢?

什么是 Mobile_Detect

include('/Mobile_Detect.php');

$detect = new Mobile_Detect;

//获取操作系统
 foreach($detect->getOperatingSystems() as $os => $regex){
 if(empty($regex)){ continue; }
 if(preg_match('/'.$regex.'/is', $detect->getUserAgent())){ echo $os; break; }
 }

//获取手机设备
 $all_devices = array_merge($detect->getPhoneDevices(), $detect->getTabletDevices());
 foreach($all_devices as $device => $regex){
 if(empty($regex)){ continue; }
 if(preg_match('/'.$regex.'/is', $detect->getUserAgent())){ echo $device; break; }
 }

原文来自http://blog.wpjam.com/m/get-os-device-via-mobile-detect/