Instant gratification!

Optimizing sites for mobile browsers is becoming much more common (even necessary) these days — but many times a mobile-optimized site just isn’t in the project scope or budget.

Fortunately, there’s several things you can do to help out your pint-sized-browser-friends without building a fully-optimized site. One of the easiest is to display a phone number that when clicked, automatically dials the number. However, since regular browsers aren’t made for dialing, the trick is displaying a plain-ole-number for them. So here’s a quick-tip to accomplish our goal.

Before we get to the goodies, I’m fairly certain this will only work for Webkit (iPhone & Android) browsers, as they seem to be the only ones that currently recognize the tel: hyperlink.

We’ll be using straight-up PHP so we can do this server-side, although I know there’s other ways to do this (like jQuery).

<?php
if( strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') ||
    strstr($_SERVER['HTTP_USER_AGENT'],'Android')
    ){ echo '<a href="tel:555-373-9537"; target="_self">555 373 9537</a>'; }
else { echo '555 373 9537'; }
?>

Bingo! Desktop browsers are happy — mobile browsers rejoice!