Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:57:22+00:00 2026-05-14T04:57:22+00:00

My new phone does not recognize a phone number unless its area code matches

  • 0

My new phone does not recognize a phone number unless its 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:57:23+00:00Added an answer on May 14, 2026 at 4:57 am

    A little more digging on my own revealed the issues with the regex in my question. The problem is with duplicate contacts in the csv.

    Example:
    (208) 555-5555, 555-5555

    After the first pass becomes:

    2085555555, 208555555

    and After the second pass becomes
    2082085555555, 2082085555555

    I worked around this by changing the replacement regex to:

    //add escapes for special characters
    $pattern = preg_replace("/\(|\)|\-|\./","\\\\$0",$value);
    //add delimiters, and optional area code
    $pattern = "/(\(?[0-9]{3}\)?)? ?" . $pattern . "/";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Microsoft recently released tools and documentation for its new Phone 7 platform, which to
After a battle getting the correct location for pear my code does not generate
I'm working on Windows Phone 7 which does not support features like CUDA or
I want to Send AT Command to Android phone. I know SDK does not
I recently bought a new phone (Motorola Electrify) and I'm trying to connect it
i would like to buy a new cell phone that runs Android. From what
Where can I find the SDK for new Windows Phone 7?
All, I am very new to Windows Phone and really don't know where to
I am new to the windows phone development, and I am struggling to get
I am new in Pocket PC and Smart phone. so can any one help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.