Solved

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

Posted on 2013-01-24
7
372 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
How about:

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

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
<?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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 108

Expert Comment

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

Anyway, thanks for the points, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now