PHP simple way to judge mobile devices _php Tips

Source: Internet
Author: User
Tags php programming php regular expression

This article describes a simple way to judge a mobile device in PHP. Share to everyone for your reference, specific as follows:

Now the mobile Internet is increasingly being sent to a lot of sites are popular mobile phone browsing, in order to better let the Web page on the mobile side display, we have chosen to use CSS Media Query production response template, but this also has drawbacks, such as the structure of some sites are CMS type, too much content to show, and the use of CSS Media query design response, only hidden but still loaded, in order to allow the phone to display the content faster, we can use this PHP to judge the phone device code, use this code can be very convenient to display or not display custom content.

This is the PHP judge cell phone device function code, copied to the PHP function library call:

<?php function Is_mobile () {$user _agent = $_server [' http_user_agent ']; 
      $mobile _browser = Array ("Mqqbrowser",//Mobile QQ Browser "Opera mobi",//Mobile phone Opera "Juc", "Iuc",//UC Browser
      "Fennec", "ios", "applewebkit/420", "applewebkit/525", "applewebkit/532", "ipad", "iphone", "iPAQ", "ipod", "Iemobile", "Windows CE",//Windows Phone "240x320", "48 0x640 "," Acer "," Android "," anywhereyougo.com "," Asus "," Audio "," BlackBerry ","
      Blazer "," Coolpad "," Dopod "," Etouch "," Hitachi "," HTC "," Huawei "," Jbrowser ", "Lenovo", "LG", "lg-", "lge-", "Lge", "Mobi", "Moto", "Nokia", "phone
      "," Samsung "," Sony "," Symbian "," tablet "," Tianyu "," WAP "," XDA "," Xde ",
  "ZTE");
  $is _mobile = false; foreach ($mobile _browseR as $device) {if (Stristr ($user _agent, $device)) {$is _mobile = true;
    Break
} return $is _mobile;

 ?>//This is the calling code, which can be combined with if judgment: <?php if (Is_mobile ()):?>//Set the contents of the mobile phone <?php endif;?>

More about PHP Interested readers can view the site topics: "PHP Network Programming Skills Summary", "Php Curl Usage Summary", "PHP Array" operation Skills Encyclopedia, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", " PHP Data structure and algorithm tutorial, "PHP Programming Algorithm Summary", "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.