Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.72 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. stop at the first string matched using strpos() in PHP
  2. private $arrAgent = array(
  3.     'sony',
  4.     'symbian',
  5.     'nokia',
  6.     'samsung',
  7.     'mobile',
  8.     'windows ce',
  9.     'blackberry',
  10.     'ericsson',
  11.     'danger',
  12.     'palm',
  13.     'series60',
  14.     'palmsource',
  15.     'pocketpc',
  16.     'smartphone',
  17.     'vodafone',
  18.     'iphone',
  19.     'ipad',
  20.     'android'
  21.     );
  22.        
  23. private function detectMobileAgent() {
  24.  
  25.     if ($this->MobileDevice === false) {
  26.  
  27.         foreach ($this->arrAgent as $key => $value) {
  28.  
  29.             if (strpos(Server::userAgent(), $value) !== false) {
  30.                 $this->MobileDevice = true;
  31.                 // echo $value;
  32.                 break;
  33.             }
  34.         }
  35.     }
  36. }
  37.        
  38. mozilla/5.0 (ipad; u; cpu os 4_3_2 like mac os x; en-us) applewebkit/533.17.9 (khtml, like gecko) version/5.0.2 mobile/8h7 safari/6533.18.5
  39.        
  40. $arrAgent = array(
  41.   'sony',
  42.   'symbian',
  43.   'nokia',
  44.   'samsung',
  45.   'mobile',
  46.   'windows ce',
  47.   'blackberry',
  48.   'ericsson',
  49.   'danger',
  50.   'palm',
  51.   'series60',
  52.   'palmsource',
  53.   'pocketpc',
  54.   'smartphone',
  55.   'vodafone',
  56.   'iphone',
  57.   'android',
  58.   'ipad'
  59. );
  60.  
  61. $agent = 'mozilla/5.0 (ipad; u; cpu os 4_3_2 like mac os x; en-us) applewebkit/533.17.9 (khtml, like gecko) version/5.0.2 mobile/8h7 safari/6533.18.5';
  62.  
  63. $pattern = '/((' . implode(')|(', $arrAgent) . '))/';
  64.  
  65. $found = preg_match($pattern, $agent, $matches);
  66. if (!$found) {
  67.   print 'not a mobile device';
  68.   exit;
  69. }
  70.  
  71. print 'device: ' . $matches[0];
  72.        
  73. if ($this->MobileDevice === false && strpos(Server::userAgent(), 'ipad') === false) {
  74.  
  75.     foreach ($this->arrAgent as $key => $value) {
  76.  
  77.         if (strpos(Server::userAgent(), $value) !== false) {
  78.             $this->MobileDevice = true;
  79.             // echo $value;
  80.             break;
  81.         }
  82.     }
  83. }