?
Solved

change the pattern so 10 numbers in a row is a phone number

Posted on 2013-01-24
7
Medium Priority
?
382 Views
Last Modified: 2013-01-26
<?php
    function scrape_data_field_phone($phone_content_raw)
  {
  
    $phone_content_raw = str_replace(' one ', 'one', $phone_content_raw);
    $phone_content_raw = str_replace(' two ', 'two', $phone_content_raw);
    $phone_content_raw = str_replace(' three ', 'three', $phone_content_raw);
    $phone_content_raw = str_replace(' four ', 'four', $phone_content_raw);
    $phone_content_raw = str_replace(' five ', 'five', $phone_content_raw);
    $phone_content_raw = str_replace(' six ', 'six', $phone_content_raw);
    $phone_content_raw = str_replace(' seven ', 'seven', $phone_content_raw);
    $phone_content_raw = str_replace(' eight ', 'eight', $phone_content_raw);
    $phone_content_raw = str_replace(' nine ', 'nine', $phone_content_raw);
    $phone_content_raw = str_replace(' zero ', 'zero', $phone_content_raw);
  
    
    $phone_content_raw = str_replace('one', '1', $phone_content_raw);
    $phone_content_raw = str_replace('two', '2', $phone_content_raw);
    $phone_content_raw = str_replace('three', '3', $phone_content_raw);
    $phone_content_raw = str_replace('four', '4', $phone_content_raw);
    $phone_content_raw = str_replace('five', '5', $phone_content_raw);
    $phone_content_raw = str_replace('six', '6', $phone_content_raw);
    $phone_content_raw = str_replace('seven', '7', $phone_content_raw);
    $phone_content_raw = str_replace('eight', '8', $phone_content_raw);
    $phone_content_raw = str_replace('nine', '9', $phone_content_raw);
    $phone_content_raw = str_replace('zero', '0', $phone_content_raw);
  
    $phone_content  = $phone_content_raw;
      
      $phone_pattern = '/([0-9]{0,1})?[-.\s]?(\(?([0-9]{3})\)?[-.\s]?)?([0-9]{3})[-.\s]+([0-9]{4})([\s]?[x|ext]?[\s]?([0-9]+))?/s';
      preg_match($phone_pattern, $phone_content , $phone_matches);
      
    if(!empty($phone_matches))
    {            
      $area_code = !empty($phone_matches[3])?$phone_matches[3].'-':'';
      $phone = $area_code.$phone_matches[4].'-'.$phone_matches[5];
      $ext = !empty($phone_matches[7])?' x'.$phone_matches[7]:'';
      $phone = $phone . $ext;            
    }
    
    return $phone;
  }
  
  echo scrape_data_field_phone("2158057698");
  echo scrape_data_field_phone("215-805-7698");

Open in new window



only the second echo had output and not the first

how to change the pattern so 10 numbers in a row is a phone number
0
Comment
Question by:rgb192
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38816748
How about:

$phone_pattern = '/(?:\d\D*){10}/';

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 38816873
Please post the test data that you might be using with that code above, thanks.

Here is how to normalize a phone number.

<?php // RAY_validate_phone_numbers.php
error_reporting(E_STRICT);

// A FUNCTION TO VALIDATE A PHONE NUMBER AND RETURN A NORMALIZED STRING
// MAN PAGE: http://discuss.fogcreek.com/joelonsoftware3/default.asp?cmd=show&ixPost=102667&ixReplies=15
function strtophone($phone, $format=FALSE, $dlm='-')
{
    // HANDLE INPUT LIKE 1-800-BIG-DOGS
    $phone = strtoupper($phone);
    if (preg_match('/[A-Z]/', $phone))
    {
        $phone = str_replace('A', '2', $phone);
        $phone = str_replace('B', '2', $phone);
        $phone = str_replace('C', '2', $phone);

        $phone = str_replace('D', '3', $phone);
        $phone = str_replace('E', '3', $phone);
        $phone = str_replace('F', '3', $phone);

        $phone = str_replace('G', '4', $phone);
        $phone = str_replace('H', '4', $phone);
        $phone = str_replace('I', '4', $phone);

        $phone = str_replace('J', '5', $phone);
        $phone = str_replace('K', '5', $phone);
        $phone = str_replace('L', '5', $phone);

        $phone = str_replace('M', '6', $phone);
        $phone = str_replace('N', '6', $phone);
        $phone = str_replace('O', '6', $phone);

        $phone = str_replace('P', '7', $phone);
        $phone = str_replace('Q', '7', $phone);
        $phone = str_replace('R', '7', $phone);
        $phone = str_replace('S', '7', $phone);

        $phone = str_replace('T', '8', $phone);
        $phone = str_replace('U', '8', $phone);
        $phone = str_replace('V', '8', $phone);

        $phone = str_replace('W', '9', $phone);
        $phone = str_replace('X', '9', $phone);
        $phone = str_replace('Y', '9', $phone);
        $phone = str_replace('Z', '9', $phone);
    }

    // DISCARD NON-NUMERIC CHARACTERS
    $phone = preg_replace('/[^0-9]/', '', $phone);

    // DISCARD A LEADING '1' FROM NUMBERS ENTERED LIKE 1-800-555-1212
    if (substr($phone,0,1) == '1') $phone = substr($phone,1);

    // IF LESS THAN TEN DIGITS, IT IS INVALID
    if (strlen($phone) < 10) return FALSE;

    // IF IT STARTS WITH '0' OR '1' IT IS INVALID, SECOND DIGIT CANNOT BE '9' (YET)
    if (substr($phone,0,1) == '0') return FALSE;
    if (substr($phone,0,1) == '1') return FALSE;
    if (substr($phone,1,1) == '9') return FALSE;

    // ADD OTHER TESTS HERE AS MAY BE NEEDED

    // IF NOT FORMATTED
    if (!$format) return $phone;

    // ISOLATE THE COMPONENTS OF THE PHONE NUMBER
    $ac = substr($phone,0,3); // AREA
    $ex = substr($phone,3,3); // EXCHANGE
    $nm = substr($phone,6,4); // NUMBER
    $xt = substr($phone,10);  // EXTENSION

    // STANDARDIZE THE PRINTABLE FORMAT OF THE PHONE NUMBER LIKE 212-555-1212-1234
    $formatted_phone = $ac . $dlm . $ex . $dlm . $nm;
    if ($xt != '') $formatted_phone .= $dlm . $xt;
    return $formatted_phone;
}




// IF ANYTHING IS POSTED?
if (!empty($_POST["phone"]))
{

// VALIDATE PHONE
    if (!$phone = strtophone($_POST["phone"], TRUE))
    {
        echo "BOGUS: {$_POST["phone"]} \n";
    } else
    {

// SHOW THE FORMATTED PHONE
        echo "VALID: $phone\n";
    }
}
// END PHP, PUT UP THE FORM TO TEST PHONE NUMBERS
?>
<form method="post">
ENTER A PHONE NUMBER:
<input name="phone" /><br/>
<input type="submit" />
</form>
TRY SOME OF THESE (COPY AND PASTE):
<br/>1-800-5551212
<br/>866-Big-Dogs
<br/>202-537-7560
<br/>123456789
<br/>703-356-5300 x2048
<br/>(212) 555-1212
<br/>1 + (212) 555-1212
<br/>1 (292) 226-7000

Open in new window

When you post the test data I'll show you how to use this, based on the test data input that you've created.  Best regards, ~ray
0
 

Author Comment

by:rgb192
ID: 38817147
<?php // RAY_validate_phone_numbers.php
error_reporting(E_STRICT);

// A FUNCTION TO VALIDATE A PHONE NUMBER AND RETURN A NORMALIZED STRING
// MAN PAGE: http://discuss.fogcreek.com/joelonsoftware3/default.asp?cmd=show&ixPost=102667&ixReplies=15
function strtophone($phone, $format=FALSE, $dlm='-')
{
    // HANDLE INPUT LIKE 1-800-BIG-DOGS
    $phone = strtoupper($phone);
    if (preg_match('/[A-Z]/', $phone))
    {
        $phone = str_replace('A', '2', $phone);
        $phone = str_replace('B', '2', $phone);
        $phone = str_replace('C', '2', $phone);

        $phone = str_replace('D', '3', $phone);
        $phone = str_replace('E', '3', $phone);
        $phone = str_replace('F', '3', $phone);

        $phone = str_replace('G', '4', $phone);
        $phone = str_replace('H', '4', $phone);
        $phone = str_replace('I', '4', $phone);

        $phone = str_replace('J', '5', $phone);
        $phone = str_replace('K', '5', $phone);
        $phone = str_replace('L', '5', $phone);

        $phone = str_replace('M', '6', $phone);
        $phone = str_replace('N', '6', $phone);
        $phone = str_replace('O', '6', $phone);

        $phone = str_replace('P', '7', $phone);
        $phone = str_replace('Q', '7', $phone);
        $phone = str_replace('R', '7', $phone);
        $phone = str_replace('S', '7', $phone);

        $phone = str_replace('T', '8', $phone);
        $phone = str_replace('U', '8', $phone);
        $phone = str_replace('V', '8', $phone);

        $phone = str_replace('W', '9', $phone);
        $phone = str_replace('X', '9', $phone);
        $phone = str_replace('Y', '9', $phone);
        $phone = str_replace('Z', '9', $phone);
    }

    // DISCARD NON-NUMERIC CHARACTERS
    $phone = preg_replace('/[^0-9]/', '', $phone);

    // DISCARD A LEADING '1' FROM NUMBERS ENTERED LIKE 1-800-555-1212
    if (substr($phone,0,1) == '1') $phone = substr($phone,1);

    // IF LESS THAN TEN DIGITS, IT IS INVALID
    if (strlen($phone) < 10) return FALSE;

    // IF IT STARTS WITH '0' OR '1' IT IS INVALID, SECOND DIGIT CANNOT BE '9' (YET)
    if (substr($phone,0,1) == '0') return FALSE;
    if (substr($phone,0,1) == '1') return FALSE;
    if (substr($phone,1,1) == '9') return FALSE;

    // ADD OTHER TESTS HERE AS MAY BE NEEDED

    // IF NOT FORMATTED
    if (!$format) return $phone;

    // ISOLATE THE COMPONENTS OF THE PHONE NUMBER
    $ac = substr($phone,0,3); // AREA
    $ex = substr($phone,3,3); // EXCHANGE
    $nm = substr($phone,6,4); // NUMBER
    $xt = substr($phone,10);  // EXTENSION

    // STANDARDIZE THE PRINTABLE FORMAT OF THE PHONE NUMBER LIKE 212-555-1212-1234
    $formatted_phone = $ac . $dlm . $ex . $dlm . $nm;
    if ($xt != '') $formatted_phone .= $dlm . $xt;
    return $formatted_phone;
}




// IF ANYTHING IS POSTED?
if (!empty($_POST["phone"]))
{

// VALIDATE PHONE
    if (!$phone = strtophone($_POST["phone"], TRUE))
    {
        echo "BOGUS: {$_POST["phone"]} \n";
    } else
    {

// SHOW THE FORMATTED PHONE
        echo "VALID: $phone\n";
    }
}
// END PHP, PUT UP THE FORM TO TEST PHONE NUMBERS
?>
<form method="post">
ENTER A PHONE NUMBER:
<input name="phone" /><br/>
<input type="submit" />
</form>
TRY SOME OF THESE (COPY AND PASTE):
<?php
echo '<h1>Ray\'s Data</h1>';
echo '<br/>1-800-5551212: '. strtophone("1-800-5551212");
echo '<br/>866-Big-Dogs: '. strtophone("866-Big-Dogs");
echo '<br/>202-537-7560: '. strtophone("202-537-7560");
echo '<br/>123456789: '. strtophone("123456789");
echo '<br/>703-356-5300 x2048: '. strtophone("703-356-5300 x2048");
echo '<br/>(212) 555-1212: '. strtophone("(212) 555-1212");
echo '<br/>1 + (212) 555-1212: '. strtophone("1 + (212) 555-1212");
echo '<br/>1 (292) 226-7000: '. strtophone("1 (292) 226-7000");

echo '<h1>rbg192\'s Data</h1>';
echo '<br/>2345678901: '. strtophone("2345678901");
echo '<br/>12345678901: '. strtophone("12345678901");
echo '<br/>two 345678901: '. strtophone("two 345678901");

Open in new window



Ray's Data

1-800-5551212: 8005551212
866-Big-Dogs: 8662443647
202-537-7560: 2025377560
123456789: 
703-356-5300 x2048: 703356530092048
(212) 555-1212: 2125551212
1 + (212) 555-1212: 2125551212
1 (292) 226-7000: 
rbg192's Data

2345678901: 2345678901
12345678901: 2345678901
two 345678901: 

Open in new window


want to work:
1 (292) 226-7000
two 345678901




@Kaufmed:
I do not know where to add this line you suggested:
$phone_pattern = '/(?:\d\D*){10}/';
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 38817446
You said, "how to change the pattern..." I assumed by that you meant change this line:

$phone_pattern = '/([0-9]{0,1})?[-.\s]?(\(?([0-9]{3})\)?[-.\s]?)?([0-9]{3})[-.\s]+([0-9]{4})([\s]?[x|ext]?[\s]?([0-9]+))?/s';

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 38818308
kaufmed
answered my question

but Ray showed me there are more potential patterns

so I think that both answers are equal

thanks
0
 

Author Comment

by:rgb192
ID: 38823107
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38823397
two 345678901: Equals what, exactly?  Is this a commonly expected client input?

Anyway, thanks for the points, ~Ray
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question