Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

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

<?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
Avatar of kaufmed
kaufmed
Flag of United States of America image

How about:

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

Open in new window

SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

<?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}/';
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

kaufmed
answered my question

but Ray showed me there are more potential patterns

so I think that both answers are equal

thanks
two 345678901: Equals what, exactly?  Is this a commonly expected client input?

Anyway, thanks for the points, ~Ray