Detect mobile browser/user agent with PHP (iPad, iPhone, Android, BlackBerry, WP7 and others)
With all this load of smartphones and tablets, it is mandatory for us as developers to optimize our content as much as possible so everybody can see it, no matter what device they are using. This is why I wrote a small but powerful function to properly detect a wide range of mobile devices and redirect to an special content accordingly.
Lets take a look:
function detect_mobile()
{
if(preg_match('/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|sagem|sharp|sie-|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i', $_SERVER['HTTP_USER_AGENT']))
return true;
else
return false;
}
This function can detect most (if not all) of all the standard “dumb” phones, smartphones, including the iPhone, iPad, Android and Windows Phone 7. Lets say you want to redirect your users to your blog if they are accessing your home page from a mobile device:
$mobile = detect_mobile();
if($mobile === true)
header('Location: blog');
Nice and easy!
Changelog:
24/03/2011 – Updated for Android
25/04/2011 – Updated for Windows Phone 7
23/05/2011 – Updated for iPod Touch
16/01/2012 – Rewritten, shortened. Deleted unnecesary checks. Added Windows CE support.


Please how can this same code to differenciate high end devices from the normal wap phones. for instance i want to redirect people using highend devices to siteA and the other people using just wap phones like nokia to pageB and people using blackberry to pageC.
Please any help would be appreciated. thanks
Can you please translate this script into javascript? Thank you so much!