如何检测网页中的移动设备 [英] How to detect a mobile device in a web page

查看:166
本文介绍了如何检测网页中的移动设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个网站,我想为移动设备使用特定的CSS规则。我想要的是以下内容:

For a website I want to use specific CSS rules for mobile devices. What I want is the following:


  • 在电话号码上创建链接,并使其可在移动设备上点击

  • 代码示例:< a href =tel:+1234567890> + 1234567890< / a> (类似于mailto:john@hotmail.com )

  • 在普通计算机上单击此链接可能会产生一条错误消息,表明他们不知道tel协议。

  • create a link on a phone number, and make it clickable for mobile devices
  • code example: <a href="tel:+1234567890">+1234567890</a> (similar to mailto:john@hotmail.com)
  • Clicking this link on a normal computer will probably produce an error message saying that they don't know the tel-protocol.

我想使用CSS在非移动设备上隐藏电话号码,并显示另一个元素数。我可以使用media =handheld选项,但它似乎Android和iOS忽略这一点。一个干净的CSS方法是首选,但如果需要Javascript(或JQuery)没有问题。

I want to use CSS to hide the phone number with the link on non-mobile devices, and display another element with a plain phone number. I could use the media="handheld" option, but it seems Android and iOS ignore this. A clean CSS-method is preferred, but it's no problem if Javascript (or JQuery) is needed.

我的问题现在关于这个tel-link,但是我可能会使用相同的方法对移动设备的样式表进行其他更改。

My question is now about this tel-link, but I probably will use the same method for other changes to the stylesheet for mobile devices.

推荐答案

这是我用来检查给定请求是否来自移动设备:

This is what i use to check if the given request comes from a mobile device:

$mobile_browser = '0';

if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $mobile_browser++;
}

if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
}    

$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda ','xda-');

if (in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
}

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
    $mobile_browser = 0;
}

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'mac') > 0) {
        $mobile_browser = 0;
}

if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'ios') > 0) {
        $mobile_browser = 1;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'android') > 0) {
        $mobile_browser = 1;
}

if($mobile_browser == 0)
{
    //its not a mobile browser
    echo"You are not a mobile browser";
} else {
    //its a mobile browser
    echo"You are a mobile browser!";
}
?>

我在我的一个配偶的帮助下,如果你有提示或更正我在评论中:)

I made this with help of a mate of mine, if you have tips or corrections for me please give in comments:)

这篇关于如何检测网页中的移动设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆