免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1981 | 回复: 1
打印 上一主题 下一主题

Mobile Detect Class [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-20 20:44 |只看该作者 |倒序浏览

Mobile Detect Class










Java代码
  1. 1.<?php<br>class Mobile_Detect  
  2. 2.{  
  3. 3.  
  4. 4.    protected $accept;  
  5. 5.    protected $userAgent;  
  6. 6.    protected $isMobile = false;  
  7. 7.    protected $isAndroid = null;  
  8. 8.    protected $isAndroidtablet = null;  
  9. 9.    protected $isIphone = null;  
  10. 10.    protected $isIpad = null;  
  11. 11.    protected $isBlackberry = null;  
  12. 12.    protected $isBlackberrytablet = null;  
  13. 13.    protected $isOpera = null;  
  14. 14.    protected $isPalm = null;  
  15. 15.    protected $isWindows = null;  
  16. 16.    protected $isWindowsphone = null;  
  17. 17.    protected $isGeneric = null;  
  18. 18.    protected $devices = array(  
  19. 19.        "android" => "android.*mobile",  
  20. 20.        "androidtablet" => "android(?!.*mobile)",  
  21. 21.        "blackberry" => "blackberry",  
  22. 22.        "blackberrytablet" => "rim tablet os",  
  23. 23.        "iphone" => "(iphone|ipod)",  
  24. 24.        "ipad" => "(ipad)",  
  25. 25.        "palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",  
  26. 26.        "windows" => "windows ce; (iemobile|ppc|smartphone)",  
  27. 27.        "windowsphone" => "windows phone os",  
  28. 28.        "generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)"  
  29. 29.    );  
  30. 30.  
  31. 31.    public function __construct()  
  32. 32.    {  
  33. 33.        $this->userAgent = $_SERVER['HTTP_USER_AGENT'];  
  34. 34.        $this->accept = $_SERVER['HTTP_ACCEPT'];  
  35. 35.  
  36. 36.        if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {  
  37. 37.            $this->isMobile = true;  
  38. 38.        } elseif (strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0) {  
  39. 39.            $this->isMobile = true;  
  40. 40.        } else {  
  41. 41.            foreach ($this->devices as $device => $regexp) {  
  42. 42.                if ($this->isDevice($device)) {  
  43. 43.                    $this->isMobile = true;  
  44. 44.                }  
  45. 45.            }  
  46. 46.        }  
  47. 47.    }  
  48. 48.  
  49. 49.    /**
  50. 50.     * Overloads isAndroid() | isAndroidtablet() | isIphone() | isIpad() | isBlackberry() | isBlackberrytablet() | isPalm() | isWindowsphone() | isWindows() | isGeneric() through isDevice()
  51. 51.     *
  52. 52.     * @param string $name
  53. 53.     * @param array $arguments
  54. 54.     * @return bool
  55. 55.     */  
  56. 56.    public function __call($name, $arguments)  
  57. 57.    {  
  58. 58.        $device = substr($name, 2);  
  59. 59.        if ($name == "is" . ucfirst($device) && array_key_exists(strtolower($device), $this->devices)) {  
  60. 60.            return $this->isDevice($device);  
  61. 61.        } else {  
  62. 62.            trigger_error("Method $name not defined", E_USER_WARNING);  
  63. 63.        }  
  64. 64.    }  
  65. 65.  
  66. 66.    /**
  67. 67.     * Returns true if any type of mobile device detected, including special ones
  68. 68.     * @return bool
  69. 69.     */  
  70. 70.    public function isMobile()  
  71. 71.    {  
  72. 72.        return $this->isMobile;  
  73. 73.    }  
  74. 74.  
  75. 75.    protected function isDevice($device)  
  76. 76.    {  
  77. 77.        $var = "is" . ucfirst($device);  
  78. 78.        $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[strtolower($device)] . "/i", $this->userAgent) : $this->$var;  
  79. 79.        if ($device != 'generic' && $return == true) {  
  80. 80.            $this->isGeneric = false;  
  81. 81.        }  
  82. 82.  
  83. 83.        return $return;  
  84. 84.    }  
  85. 85.  
  86. 86.}  
复制代码
Java代码
  1. 1.Include and instantiate the class:  
  2. 2.  
  3. 3.include("Mobile_Detect.php");  
  4. 4.$detect = new Mobile_Detect();  
  5. 5.Check for a specific platform:  
  6. 6.  
  7. 7.if ($detect->isAndroid()) {  
  8. 8.    // code to run for the Google Android platform  
  9. 9.}  
  10. 10.Available methods are isAndroid(), isAndroidtablet(), isIphone(), isIpad(), isBlackberry(), isBlackberrytablet(), isPalm(), isWindowsphone(), isWindows(), isGeneric(). Alternatively, if you are only interested in checking to see if the user is using a mobile device, without caring for specific platform:  
  11. 11.  
  12. 12.if ($detect->isMobile()) {  
  13. 13.    // any mobile platform  
  14. 14.}
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-20 20:44 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP