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 7 4624 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 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;
}
?> 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
<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;
?>
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
<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 .
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Earl |
last post by:
I'm curious if there are others who have a better method of
accepting/parsing phone numbers. I've used a couple of different techniques
that are functional but I can't really say that I'm totally...
|
by: JackM |
last post by:
I'm still working on validating the phone numbers that are entered on a
form but have come across a problem I don't understand how to fix. I can
handle most instances when it's in regular US...
|
by: venu |
last post by:
Hi,
I have a different requirement and it is :
I need to validate a phone number field.
It may or may not be a US phone number.
The constraints are :
***********************
# It should...
|
by: Petr Jakeš |
last post by:
I have a standard 12-key mobile phone keypad connected to my Linux
machine as a I2C peripheral. I would like to write a code which allows
the text entry to the computer using this keypad (something...
|
by: gvidak |
last post by:
Hello Guys,
Yes, another beginner in C++ and I have a homework assignment that is giving me a headache.
Can someone help me with the following problem?
I have a text file with names, phone numbers...
|
by: lilOlMe |
last post by:
I'd love to be able to validate phone numbers in my software but my product is being used world wide.
Not everyone's phone number is formatted like USA/Canada formats theirs.
I've found a few...
|
by: lim4801 |
last post by:
I am currently in doing a program which is given by my tutor:
Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
|
by: Blue Streak |
last post by:
Hello, Folks!
Does anyone know of a website that lists the local phone number
formats for each country?
TIA...
|
by: Propoflady |
last post by:
My contacts can have as many as five or six phone numbers - is it
possible to make a query that puts the name, and each phone number
after it on the same line - for this reason I have two tables -...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |