Making your blog display more mobile friendly
12/31/09
Making your blog display more mobile friendly
After adding the twitterlution plugin to my blog, I thought it would be a good idea to make the blog a little more mobile friendly. So after doing some research, I came across Mike Industries' post about making your site mobile friendly and Lightweight Device-Detection in PHP post.
After some creative combining, I have updated this blog to display in a different format if you access the blog from a mobile device. I know that the site is much more lightweight now, but the format needs tweaked to be more usable. That being said, here is what I did.
I started with the sample provided by Mike Industries and pretty much followed it exactly as described in the post. Then added this code snippet from Lightweight Device-Detection in PHP post
PHP:
function is_mobile(){ | |
$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 isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT'])); | |
} |
to the global_prepend.php file and then I updated the callback function to use the mobile detection and handle any calls to the site without the www.
PHP:
<?php | |
# from <a href="http://www.mikeindustries.com/blog/archive/2005/07/make-your-site-mobile-friendly">http://www.mikeindustries.com/blog/archive/2005/07/make-your-site-mobile-friendly</a> | |
function callback($buffer) { | |
if(( $_SERVER['SERVER_NAME'] == 'mobile.sitename.com' ) || ( is_mobile())) { | |
$buffer = str_replace('http://www.sitename.com', 'http://mobile.sitename.com', $buffer); | |
$buffer = str_replace('http://sitename.com', 'http://mobile.sitename.com', $buffer); | |
$buffer = preg_replace('/[\n\r\t]+/', '', $buffer); | |
$buffer = preg_replace('/\s{2,}/', ' ', $buffer); | |
$buffer = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5', $buffer); | |
$buffer = preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $buffer); | |
$buffer = preg_replace('/<\/head>/i', '<meta name="robots" content="noindex, nofollow"></head>', $buffer); | |
} | |
return $buffer; | |
} | |
| |
# from <a href="http://mobiforge.com/developing/story/lightweight-device-detection-php">http://mobiforge.com/developing/story/lightweight-device-detection-php</a> | |
function is_mobile(){ | |
$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 isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT'])); | |
} | |
| |
ob_start("callback"); | |
?> |
Hopefully you all find this helpful.
Cheers,
Dave
Pingbacks:
No Pingbacks for this post yet...
This post has 5 feedbacks awaiting moderation...
Scripts
This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.
Search
Follow Me:
Categories
- All
- Web Technologies (2)
- PHP (1)
- Windows Scripting (37)
- Batch Scripts (13)
- Wsh Scripts (23)
Archives
- December 2009 (2)
- March 2009 (2)
- February 2009 (3)
- January 2009 (2)
- December 2008 (4)
- November 2008 (1)
- October 2008 (1)
- February 2008 (1)
- December 2007 (3)
- July 2007 (1)
- April 2007 (1)
- February 2007 (2)
- More...
Misc
Who's Online?
- Guest Users: 1




