?
Solved

only want the first phone number in $text

Posted on 2013-01-29
6
Medium Priority
?
552 Views
Last Modified: 2013-01-30
only want the first phone number
do not want the
8888888888888888888888

<?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='8888888888888888888888';

echo '<br/>1-800-5551212: '. strtophone("1-800-5551212 $string");
echo '<br/>866-Big-Dogs: '. strtophone("866-Big-Dogs $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

0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
6 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38833872
Please post the test data -- just the test data, not the code that doesn't work.  We only need to see the test data that you have created.  We especially need to see the part that shows us samples that should work and samples that should be filtered out.  You may also want to post the rules that describe why some samples work and some would be filtered out.

You can use this reference as you prepare the test data: SSCCE

Thanks, ~Ray
0
 

Author Comment

by:rgb192
ID: 38834173
test data:

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
8888888888888888888888
is added to all the output that works (making the output not work)
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 38834826
Please see http://www.laprbass.com/RAY_temp_rgb192.php

This gets close, but the original translation gets bollixed up with the "x" abbreviation for the extension.  I cannot imagine where all the '8' come from - that has the smell of a programming error upstream.  If this were my task, I think I would eliminate the option to have letters in the phone number.  That seems to be at least one major part of the confusion.

<?php // RAY_temp_insoftservice.php
error_reporting(E_ALL);
echo '<pre>';


// TEST DATA
$str = <<<END
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
END;

// OPERATE ON EACH LINE TO REMOVE TRAILING 8
$arr = explode(PHP_EOL, $str);
foreach ($arr as $key => $xyz)
{
    $arr[$key] = rtrim(trim($xyz), '8');
}
$str = implode(PHP_EOL, $arr);
var_dump($str);

Open in new window

Best of luck with it, ~Ray
0
Industry Leaders: 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 5

Expert Comment

by:Php_expert
ID: 38835232
if you dont want 8888888888888888888888 then why you are passing $string='8888888888888888888888' to the following code


$string='8888888888888888888888';

echo '<br/>1-800-5551212: '. strtophone("1-800-5551212 $string");
echo '<br/>866-Big-Dogs: '. strtophone("866-Big-Dogs $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");

$string = '';
will get the correct output.

<?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='';

echo '<br/>1-800-5551212: '. strtophone("1-800-5551212 $string");
echo '<br/>866-Big-Dogs: '. strtophone("866-Big-Dogs $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

0
 
LVL 5

Assisted Solution

by:Php_expert
Php_expert earned 1000 total points
ID: 38835252
Or you can try with following modified code:

<?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);
	$phone = rtrim(trim($phone), '8');
  }
  //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='8888888888888888888888';

echo '<br/>1-800-5551212: '. strtophone("1-800-5551212 $string");
echo '<br/>866-Big-Dogs: '. strtophone("866-Big-Dogs $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

0
 

Author Closing Comment

by:rgb192
ID: 38835423
thank you

yes you both answered the question

I created a new question where $string is more text (not an error with many 8's)

and with Ray's advice, eliminated 1800-big-dogs which he taught me.

I want the first phone number in a text block

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28014021.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 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