Handy Dandy PHP script to clean phone numbers

I needed a quick way to clean a bunch of phone numbers, I wanted to do it in excel but excel doesn’t do PHP, and the example I found was in PHP and it seemed to work well. So I used textmate, and pulled together the basic script with a fopen command to suck up a file from the disk and output a simple list to the screen (console output from TextMate) and then I could copy and paste it right back into excel.

Ingredients needed:
a bunch of messy numbers

The numbers like this, with all different formats

no number
(510) 250-0907
(510) 844-0233
510.845.7548 ext 151
510-388-3198 (cell)
no number

Save them on your desktop, and then create this file in textmate:
(excuse the lousy formatting, wordpress (or maybe this theme) is messing with the formatting and colors. I’ve attached the php file which should retain the formatting)

// gets the file
$handle = @fopen("/Users/xxx/Desktop/phone1.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$clean = phone_clean($buffer);
$number = $clean[0];
$ext = $clean[1];
print $number.$ext."\n";
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
function phone_clean($data){
// Cleans phone numbers and strips extensions
// Returns array [number,extension]
$pattern = '/\D*\(?(\d{3})?\)?\D*(\d{3})\D*(\d{4})\D*(\d{1,8})?/';
if (preg_match($pattern, $data, $match)){
if ($match[3]){
if ($match[1]){
$num = $match[1].'-'.$match[2].'-'.$match[3];
$num = $match[2].'-'.$match[3];

$num = NULL;
$match[4] ? $ext = " x".$match[4] : $ext = NULL;
$num = NULL;
$ext = NULL;
$clean = array($num,$ext);
return $clean;

Set it up with the file location and name (see phone1.txt above) and run it from textmate by going bundle-php-run

cleanphone script (rename to php to have textmate treat it properly)

Thanks to Chris Nanney’s Post for source material including the fantastic regexp work dealing with a wide variety of data.

Leave a Reply