By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,199 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,199 IT Pros & Developers. It's quick & easy.

parsing phone numbers

P: n/a
Hi,

Using php 4.4.4, I am looking to parse a US phone number string into 3
smaller strings -- area code, the first part of the phone number (3
digits) and the last part of the phone number (4 digits). The only
given is that there will be 10 digits in the string, but people may
include spaces, parens, or other characters. These are all strings
I'd expect

$p = "5558675309";
$p = "(312) 123-4567";
$p = "512-234-5678";

Thanks for any code snippets, - Dave

May 7 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
la***********@zipmail.com schrieb:
Hi,

Using php 4.4.4, I am looking to parse a US phone number string into 3
smaller strings -- area code, the first part of the phone number (3
digits) and the last part of the phone number (4 digits). The only
given is that there will be 10 digits in the string, but people may
include spaces, parens, or other characters. These are all strings
I'd expect

$p = "5558675309";
$p = "(312) 123-4567";
$p = "512-234-5678";

Thanks for any code snippets, - Dave
<?php
$a = "(312) 123-4567";
$input = str_replace(array(' ','-',')','('), '', $a);
if (preg_match('/(\d{3})(\d{3})(\d{4})/', $input, $output)) {
var_dump($output);
} else {
print "wrong format!";
}
?>

hth,
Roy
May 7 '07 #2

P: n/a
la***********@zipmail.com wrote:
Hi,

Using php 4.4.4, I am looking to parse a US phone number string into 3
smaller strings -- area code, the first part of the phone number (3
digits) and the last part of the phone number (4 digits). The only
given is that there will be 10 digits in the string, but people may
include spaces, parens, or other characters. These are all strings
I'd expect

$p = "5558675309";
$p = "(312) 123-4567";
$p = "512-234-5678";
This may not be the best option but it's what I sometimes use. You
should be able to modify it for your purposes:

<?php

//process the phone number (USA only) and strip brackets and add dashes
where needed. Output: 555-555-5555
$phone_number = $_POST['phone'];
$dash = "-";
$ext = $_POST['ext'];
$ext1 = " ext";

$pattern = '/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})$/';
if (preg_match($pattern, $phone_number, $matches)) {
// we have a match, dump sub-patterns to $matches
$phone_number = $matches[0]; // original number
$area_code = $matches[1]; // 3-digit area code
$exchange = $matches[2]; // 3-digit exchange
$number = $matches[3]; // 4-digit number
}
if ($ext == "") {
$phone = $area_code . $dash . $exchange . $dash . $number;
}else{
$phone = $area_code . $dash . $exchange . $dash . $number . $ext1 . $ext;
}

?>

May 7 '07 #3

P: n/a
la***********@zipmail.com wrote:
Using php 4.4.4, I am looking to parse a US phone number string into 3
smaller strings -- area code, the first part of the phone number (3
digits) and the last part of the phone number (4 digits). The only
given is that there will be 10 digits in the string, but people may
include spaces, parens, or other characters.
Are you trying to pull a phone number out of a longer sequence of text
that may contain other numeric things too, or are you trying to parse it
from a dedicated phone number field?

If the former, the two examples already posted look OK. If the latter, then
you can probably afford to be more forgiving. Here is some code that ought
to be more flexible:

<?php
$phone = '2-45 67598 1...2';
$parsed = parse_phone($phone);
print_r($parsed);

function parse_phone ($string, $country='us')
{
if ($country != 'us')
die("Not implemented yet!");
$retval = array();

$string = preg_replace('/[^0-9]/', '', $string);

if (strlen($string)<10)
$retval['Warning'] = "Phone number too short.";

if (strlen($string)>10)
$retval['Warning'] = "Phone number too long. "
. "Possible extension included.";

$retval['AREA'] = substr($string, 0, 3);
$retval['START'] = substr($string, 3, 3);
$retval['FINISH'] = substr($string, 6, 4);
$retval['EXT'] = substr($string, 10);

return $retval;
}
?>

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 8 '07 #4

P: n/a
<comp.lang.php>
<la***********@zipmail.com>
<7 May 2007 12:56:49 -0700>
<11*********************@y80g2000hsf.googlegroups. com>
$p = "5558675309";
$p = "(312) 123-4567";
$p = "512-234-5678";

Thanks for any code snippets
What you could do is strip everything from the phone number string
except the 0123456789 and then reform it to anything you want .

The substr() stuff hasnt been checked .


<?php

$p="(312) 123-4567";

$dummy=$p; include('the_file_below.php'); $p=$dummy;

$p1=substr($p,0,3);
$p2=substr($p,4,3);
$p3=substr($p,9,4);

$poop="(" . $p1 . ")" . " " . $p2 . "-" . $p3;

?>


<?php

$ok="1234567890";

$zstring=strlen($ok);

$zdummy=strlen($dummy);

$jazz=0;
while ($jazz<256)
{
$homer[$jazz]=0;
$jazz=$jazz+1;
}

$jazz=0;
while ($jazz<$zstring)
{
$junk=substr($ok,$jazz,1); $qaz=ord($junk); $homer[$qaz]=1;
$jazz=$jazz+1;
}

$gimmie="";

if ($zdummy>0)
{

$jazz=0;
while ($jazz<$zdummy)
{
$junk=substr($dummy,$jazz,1); $qaz=ord($junk);
if ($homer[$qaz]==1) {$gimmie=$gimmie . $junk;}
$jazz=$jazz+1;
}

}

$dummy=$gimmie;

?>
May 8 '07 #5

P: n/a
Krustov wrote:
$jazz, $junk, $zdummy, $qaz, $zstring, $gimmie
WTF?! This isn't Scrabble[TM]; you don't get bonus points for including Z,
Q and J in your variable names.

As far as I can tell, your entire 40-line include file can be replaced with:

$p = preg_replace('/[^0-9]/', '', $p);

which is only 40 characters.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 9 '07 #6

P: n/a
<comp.lang.php>
<Toby A Inkster>
<Wed, 9 May 2007 09:53:03 +0100>
<fo************@ophelia.g5n.co.uk>
$jazz, $junk, $zdummy, $qaz, $zstring, $gimmie

WTF?! This isn't Scrabble[TM]; you don't get bonus points for including Z,
Q and J in your variable names.

As far as I can tell, your entire 40-line include file can be replaced with:

$p = preg_replace('/[^0-9]/', '', $p);

which is only 40 characters.
The $ok string can contain any characters and its a lot easier and
faster to duplicate/rename the file than it is to learn expressions .

$toby seems like a good variable name .
May 9 '07 #7

P: n/a
Krustov wrote:
>As far as I can tell, your entire 40-line include file can be replaced with:
$p = preg_replace('/[^0-9]/', '', $p);
which is only 40 characters.

The $ok string can contain any characters and its a lot easier and
faster to duplicate/rename the file than it is to learn expressions .
You think it's faster to duplicate, rename and modify that file than it is
to change this:

$p = preg_replace('/[^0-9]/', '', $p);

to, say, this:

$p = preg_replace('/[^AaEeIiOoUu]/', '', $p);

?!

(That regular expression removes everything except vowels.)

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 9 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.