Solved

want first phone number in a text block

Posted on 2013-01-30
6
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 56

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 56

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 56

Expert Comment

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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

756 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