Mobile Detect是用于检测移动设备(包括平板电脑)的轻量级PHP类。它使用与特定HTTP头组合的User-Agent字符串来检测移动环境。
您可以将此脚本视为RESS(具有服务器端组件的响应式Web设计)运动的一部分。您可以通过阅读这些文章了解更多的主题:使用服务器端增强的响应设计和RESS:响应式设计+服务器端组件来改进移动支持。
您还可以使用手机通过使用任何的检测3 次第三方插件可用于:WordPress的,Drupal的是,Joomla,Magento的,等等。
以下是一些代码示例:
//包含并实例化类。
require_once'Mobile_Detect.php ' ;
$ detect = new Mobile_Detect ;
//任何移动设备(手机或平板电脑)。
if ($ detect - > isMobile ()){
}
//任何平板电脑设备。
if ($ detect - > isTablet ()){
}
//排除平板电脑
if ($ detect - > isMobile ()&& !$ detect - > isTablet ()){
}
//借助魔术方法检查一个特定的平台:
if ($ detect - > isiOS ()){
}
if ($ detect - > isAndroidOS ()){
}
//替代方法is()用于检查特定的属性。
//警告:此方法在BETA中,某些关键字属性将来会更改。
$ detect - > 是('Chrome' )
$ detect - > 是('iOS' )
$ detect - > 是('UC浏览器' )
// [...]
//使用setUserAgent()的批处理模式:
$ userAgents = array (
'的Mozilla / 5.0(Linux的;的Android 4.0.4; DESIRE HD构建/ IMM76D)为AppleWebKit / 535.19(KHTML,例如Gecko)铬/ 18.0.1025.166移动Safari / 535.19' ,
'BlackBerry7100i / 4.1.0 Profile / MIDP-2.0配置/ CLDC-1.1 VendorID / 103' ,
// [...]
);
foreach ($ userAgents as $ userAgent ){
$ detect - > setUserAgent ($ userAgent );
$ isMobile = $ detect - > isMobile ();
$ isTablet = $ detect - > isTablet ();
//使用力,但你想要的。
}
//获取组件的版本()。
//警告:此方法在BETA中,某些关键字属性将来会更改。
$ detect - > version ('iPad' ); // 4.3(float)
$ detect - > version ('iPhone' )// 3.1(float)
$ detect - > version ('Android' ); // 2.1(float)
$ detect - > version ('Opera Mini' ); // 5.0(float)
// [...]