Mobile Detect是一个轻量级PHP移动设备探测类,它通过HTTP Header 中的User-Agent字符串来检测移动设备。
你可以使用这个类来作为实现RESS(服务端的响应式设计组件)一部分。你可以通过以下两篇文章来获取更多RESS的信息。
Improve Mobile Support With Server-Side-Enhanced Responsive Design
RESS: Responsive Design + Server Side Components.
代码示例:
// Include and instantiate the class.
require_once ‘Mobile_Detect.php’;
$detect = new Mobile_Detect;
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
}
// Any tablet device.
if( $detect->isTablet() ){
}
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){
}
// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){
}
if( $detect->isAndroidOS() ){
}
// Alternative method is() for checking specific properties.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$detect->is(‘Chrome’)
$detect->is(‘iOS’)
$detect->is(‘UC Browser’)
// […]
// Batch mode using setUserAgent():
$userAgents = array(
‘Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19’,
‘BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103’,
// […]
);
foreach($userAgents as $userAgent){
$detect->setUserAgent($userAgent);
$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();
// Use the force however you want.
}
// Get the version() of components.
// WARNING: this method is in BETA, some keyword properties will change in the future.
$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)
// […]
Mobile Detect-PHP轻量级移动设备探测(包括平板)
- 本文链接: https://www.bigjin.com/post/mobile-detect.html
- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!