php代码适配手机端,js / php 手机端适配代码

第一、

使用js让广告适配合适的设备(当然你也可以直接让css来隐藏掉广告,但是js可以让网页的速度快一点,因为不需要去加载web端的广告了)

function is_mobile() {

var regex_match = /(nokia|iphone|android|motorola|^|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^|longcos|pantech|gionee|^|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operasmobi|operamini|320x320|240x320|176x220)/i;

var u = navigator.userAgent;

if (null == u) { return true;}

var result = regex_match.exec(u);

if (null == result) {

return false

} else {

return true

}

}

用法:

if (is_mobile()) {

/* 手机端的代码; */

}else{

/* web端的内容; */

}

第二、

通过服务器端配合前端来适配手机端,这样做对技术能力有一定的要求,但是会让你的网站变得焕然一新,这里以php为例:

1、使用服务器来抓去浏览器头,检查是否是手机端,下面给一个不错的函数

function is_mobile(){

/* returns true if one of the specified mobile browsers is detected */

$regex_match="/(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|";

$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";

$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";

$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|";

$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera*mini|320x320|240x320|176x220";

$regex_match.=")/i";

return preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));

}

用法:

if( is_mobile()){

/* 手机端的内容,如果不想手机端显示,那么这里留空即可,你可以可以设置只在手机端显示的内容 */

}else{

/* web端的内容,如果你只是想web端才能访问,那么在这里写如前端输出的内容 */

}

在服务器端,把web端才显示的内容都放进else里面,这样当手机端打开网站的时候,就不会显示凌乱了。

2、通过之前说的前端的方法进行设置即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值