有没有哪位大佬有写好的判断请求终端是否为手机端的代码

yookey

问题描述

webman中,有没有哪位大佬有写好的判断请求终端是否为手机端的代码,麻烦分享下哈,感谢!

330 4 0
4个回答

nitron

问AI

muyu
ysian
function is_mobile(){
    $userAgent = request()->header('user-agent');
    $mobileAgents  =  ["Android",  "iPhone",  "iPod",  "iPad",  "Windows  Phone",  "BlackBerry",  "SymbianOS"];
    foreach  ($mobileAgents  as  $mobileAgent)  {
        if  (strpos($userAgent,  $mobileAgent)  !==  false)  {
            return  true;
        }
    }
    return false;
}
yookey

感谢大佬们回复,这么简单折腾一下用着:

function is_mobile(){
    $userAgent = request()->header('user-agent');
    if(preg_match('/(iphone|ipod|ipad|android|blackberry|webos|windows phone|mobile)/i', $userAgent)) {
        return true;
    }
    return false;
}
  • 暂无评论
×
🔝