Solved

want first phone number in a text block

Posted on 2013-01-30
6
317 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 51

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
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 51

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 51

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now