Archive for May 9th, 2006

Collecting and displaying phone numbers on the web

by Dave Dash 09May06

Using some regular expressions we can easily convert this:

8005551212

Into this:

800.555.1212

In [PHP] we can use this function:

function format_phone($phone)
{
    return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1.$2.$3", $phone); 
}

Not terribly complicated. We can even go in reverse and remove the dots as I’ll demonstrate below. Despite the ease of going from a formatted string to a string of digits and back again, we often run into forms that aren’t usable.


Where am I?

You are currently browsing the Spindrop weblog archives for the day Tuesday, May 9th, 2006.

Longer entries are truncated. Click the headline of an entry to read it in its entirety.

May 2006
M T W T F S S
« Apr   Jun »
1234567
891011121314
15161718192021
22232425262728
293031  

need more help

If you found our tutorials and articles to be useful, but are still looking for more hands on help, consider hiring us. Find out more about how Spindrop can help you.