On Wednesday, July 3, 2002, at 12:35  PM, César Aracena wrote:

> I need to show up some data from a DB which consist of phone numbers
> with area code. They're stored like (xxxx) xxxxxxxx. What is the best
> approach to print them into some textboxes so they can be edited? I'm
> using substr but area codes (inside parenthesis) goes from 3 to 5
> numbers, so sometimes the closing parenthesis is showed and other times
> even the first number from the actual number is also showed. Can I use
> regex instead? How is it used? I tried to figure it out in the PHP
> manual, but it is NOT written in my level of English.

It would be a good idea to store the phone numbers in the DB as two 
separate columns of only numbers, for performance and for ease of 
coding.  One column for area codes (SMALLINT) and one column for the 
rest of the number (INT).  Then you can add the parentheses or reformat 
the numbers as desired.

But if the database is beyond your control, here is a regex that may 
help you:

// $number = the whole phone number record from the DB
preg_match_all('/^\((\d+)\) (\d+)$/', $number, $matches);

$area_code = $matches[1][0];
$rest_of_number = $matches[2][0];


Try that, but it's untested.



Erik




----

Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to