Using regex to fix phone numbers in a CSV with PHP

Posted by Hurpe on Stack Overflow See other posts from Stack Overflow or by Hurpe
Published on 2010-04-02T01:15:18Z Indexed on 2010/04/02 1:23 UTC
Read the original article Hit count: 488

Filed under:
|
|

My new phone does not recognize a phone number unless it's area code matches the incoming call. Since I live in Idaho where an area code is not needed for in-state calls, many of my contacts were saved without an area code. Since I have thousands of contacts stored in my phone, it would not be practical to manually update them. I decided to write the following PHP script to handle the problem. It seems to work well, except that I'm finding duplicate area codes at the beginning of random contacts.

 <?php
//the script can take a while to complete
set_time_limit(200);

function validate_area_code($number) {
    //digits are taken one by one out of $number, and insert in to $numString
    $numString = "";
    for ($i = 0; $i < strlen($number); $i++) {
        $curr = substr($number,$i,1);
        //only copy from $number to $numString when the character is numeric
        if (is_numeric($curr)) {
            $numString = $numString . $curr;
        }
    }
    //add area code "208" to the beginning of any phone number of length 7
    if (strlen($numString) == 7) {
        return "208" . $numString;
    //remove country code (none of the contacts are outside the U.S.)
    } else if (strlen($numString) == 11) {
        return preg_replace("/^1/","",$numString);
    } else {
        return $numString;
    }
}
//matches any phone number in the csv
$pattern = "/((1? ?\(?[2-9]\d\d\)? *)? ?\d\d\d-?\d\d\d\d)/";
$csv = file_get_contents("contacts2.CSV");
preg_match_all($pattern,$csv,$matches);


foreach ($matches[0] as $key1 => $value) {
    /*create a pattern that matches the specific phone number by adding slashes before possible special characters*/
    $pattern = preg_replace("/\(|\)|\-/","\\\\$0",$value);

    //create the replacement phone number
    $replacement = validate_area_code($value);

    //add delimeters 
    $pattern = "/" . $pattern . "/";

    $csv = preg_replace($pattern,$replacement,$csv);
}
echo $csv;

?>

Is there a better approach to modifying the csv? Also, is there a way to minimize the number of passes over the csv? In the script above, preg_replace is called thousands of times on a very large String.

© Stack Overflow or respective owner

Related posts about php

Related posts about regex