rgb192
asked on
want first phone number in a text block
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.
1-800-5551212: 80055512128888888888888888 888888
866-Big-Dogs: 86624436478888888888888888 888888
202-537-7560: 20253775608888888888888888 888888
703-356-5300 x2048: 70335653009204888888888888 8888888888 8
(212) 555-1212: 21255512128888888888888888 888888
1 + (212) 555-1212: 21255512128888888888888888 888888
2345678901: 23456789018888888888888888 888888
12345678901: 23456789018888888888888888 888888
two 345678901: 23456789018888888888888888 888888
where
124
is added to all the output that works (making the output not work)
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");
1-800-5551212: 80055512128888888888888888
866-Big-Dogs: 86624436478888888888888888
202-537-7560: 20253775608888888888888888
703-356-5300 x2048: 70335653009204888888888888
(212) 555-1212: 21255512128888888888888888
1 + (212) 555-1212: 21255512128888888888888888
2345678901: 23456789018888888888888888
12345678901: 23456789018888888888888888
two 345678901: 23456789018888888888888888
where
124
is added to all the output that works (making the output not work)
ASKER
>>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
how could I change the command to chop off everything after the selection
ASKER
is there a way to change this command
$phone = preg_replace('#\D#', '', $phone);
or something I could look up
$phone = preg_replace('#\D#', '', $phone);
or something I could look up
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have to look into this:
RegEx with lookaheads / lookbehinds
thank you
RegEx with lookaheads / lookbehinds
thank you
you are welcome - thank you for the points
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.