Solved

want first phone number in a text block

Posted on 2013-01-30
6
326 Views
Last Modified: 2013-02-13
I am including code that almost works because I think it may be close to the answer.
I only want the first phone number in a text block.

<?php

function strtophone($phone, $format=FALSE, $dlm='-')
{
    // HANDLE INPUT LIKE 1-800-BIG-DOGS
    $phone = strtoupper($phone);

  foreach (array('ZERO','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE') as $number => $text)
  {
    $phone = str_replace($text, $number, $phone);
  }
  //http://php.net/manual/en/function.strtr.php
 // $phone=strtr($phone,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','22233344455566677778889999');

    // DISCARD NON-NUMERIC CHARACTERS
    $phone = preg_replace('#\D#', '', $phone);

    // DISCARD A LEADING '1' FROM NUMBERS ENTERED LIKE 1-800-555-1212
    if ( $phone[0] == '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
    if ( $phone[0] == '0') return FALSE;
    if ( $phone[0] == '1') return FALSE;

  //SECOND DIGIT CANNOT BE '9' (YET) http://www.nanpa.com/area_codes/index.html
    if ( $phone[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;
}

$string=' sample text with more words.  Buy 1 get 2 4 free. ';

echo '<br/>1-800-5551212: '. strtophone("1-800-5551212 $string");
echo '<br/>202-537-7560: '. strtophone("202-537-7560 $string");
echo '<br/>703-356-5300 x2048: '. strtophone("703-356-5300 x2048 $string");
echo '<br/>(212) 555-1212: '. strtophone("(212) 555-1212 $string");
echo '<br/>1 + (212) 555-1212: '. strtophone("1 + (212) 555-1212 $string");
echo '<br/>2345678901: '. strtophone("2345678901 $string");
echo '<br/>12345678901: '. strtophone("12345678901 $string");
echo '<br/>two 345678901: '. strtophone("two 345678901 $string");  

Open in new window




1-800-5551212: 80055512128888888888888888888888
866-Big-Dogs: 86624436478888888888888888888888
202-537-7560: 20253775608888888888888888888888
703-356-5300 x2048: 7033565300920488888888888888888888888
(212) 555-1212: 21255512128888888888888888888888
1 + (212) 555-1212: 21255512128888888888888888888888
2345678901: 23456789018888888888888888888888
12345678901: 23456789018888888888888888888888
two 345678901: 23456789018888888888888888888888



where
124
is added to all the output that works (making the output not work)
0
Comment
Question by:rgb192
  • 3
  • 3
6 Comments
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 38836398
When I run the code I get

1-800-5551212: 8005551212124
202-537-7560: 2025377560124
703-356-5300 x2048: 70335653002048124
(212) 555-1212: 2125551212124
1 + (212) 555-1212: 2125551212124
2345678901: 2345678901124
12345678901: 2345678901124
two 345678901: 2345678901124

The problem is on line 17

    $phone = preg_replace('#\D#', '', $phone);

Because your suffix text has numbers in the above will take out the letters leaving the numbers behind.

What you need to do is instead find the first instance of a non-numeric and then chop the rest of the string that appears after this.

There may be issues with your extension though - so you might specifically have to look for those.
0
 

Author Comment

by:rgb192
ID: 38842213
>>What you need to do is instead find the first instance of a non-numeric and then chop the rest of the string that appears after this.

how could I change the command to chop off everything after the selection
0
 

Author Comment

by:rgb192
ID: 38851508
is there a way to change this command
$phone = preg_replace('#\D#', '', $phone);

or something I could look up
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 38852158
What you need is something that only looks at the first part of the string.

If you are going to allow alpha chars for a phone number i.e. WXYZ=9, TUV=8 etc AND numbers spelled out (one, two etc) then it complicates things because any string you append to the number will be included in the change.

You can't use a fixed length because

2125551935 and
(212)-551-1935

Are the same number but different lengths.

And finally there is the added complication of a an extension which further adds to the complexity.

What you need to know is when the telephone number ends.

There is probably a way to do this with RegEx with lookaheads / lookbehinds - but it is beyond the scope of what I am willing to tackle right now. Failing a RegEx solution I would look at writing a simple phone number parser that is intelligent enough to know when it has reached the end of a telephone number (i.e.) it has ticked off country code, area code, number and extension and therefore knows anything else is superflous.

There will probably still be ambiguous cases for instance

2123338877x123x123x123

Is the string at the end really an extension or is it a repeating string that has another meaning - in cases where you can't tell by looking at it code won't be able to do it either.

Not a solution but hopefully something to help you in the right direction.
0
 

Author Closing Comment

by:rgb192
ID: 38883523
I have to look into this:

RegEx with lookaheads / lookbehinds


thank you
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 38883566
you are welcome - thank you for the points
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
str_replace not working in php script 4 32
PHP and google maps 13 47
Download tables into separate sheets 3 28
Showing random records from database 10 37
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.

831 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