Solved

only want the first phone number in $text

Posted on 2013-01-29
6
521 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:Php_expert
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
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

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.

744 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