vincej Posted July 6, 2012 Share Posted July 6, 2012 Hi - I am storing phone numbers within the DB as a straight 10 digit int. ie 1112223333 I can search on them and it all works well. However I would like to make the output a little more friendly ie 111 222 3333 I have looked through the php arrays and there is nothing. I expect i need to use a regular expression, but I have so far failed miserably trying to make that work. Any suggestions ? Many Thanks !! Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted July 6, 2012 Share Posted July 6, 2012 There are a dozen ways to do this and a thousand tutorials. You can use either javascript or PHP. pick your method and find yourself a nice tutorial to cozy up with. Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 6, 2012 Share Posted July 6, 2012 Here is one that should work, although, it only takes the 10 digit format without any other characters (or it will break). <?php $pattern = '~([0-9]{3})([0-9]{3})([0-9]{4})~'; $replacement = '$1-$2-$3'; $number = 1112223333; echo preg_replace($pattern,$replacement,$number); //add parenthesis around the area code. $replacement = '($1)-$2-$3'; echo preg_replace($pattern,$replacement,$number); ?> Quote Link to comment Share on other sites More sharing options...
vincej Posted July 6, 2012 Author Share Posted July 6, 2012 Hi Jc ! Many Many thanks - I've always had a blind spot about regex ! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2012 Share Posted July 6, 2012 You can also do it in the query string, if you'd prefer. SELECT CONCAT( '(', SUBSTRING(field, 1, 3), ') ', SUBSTRING(field, 4, 3), '-', SUBSTRING(field, 7) ) as tel_no FROM table . . . Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 6, 2012 Share Posted July 6, 2012 @jcbones, since the value is only numbers then there is no need to the capture expressions to only be numbers. You could just as easily use $pattern = '~(.{3})(.{3})(.{4})~'; However, in this case RegEx might be overkill since you can just use substr(). It might look like it's more complicated, but it's probably more efficient. Here's my two cents function formatPhone($p) { $phone = substr($p, 0, 3) . ' ' . substr($p, 3, 3) . ' ' . substr($p, 6); return $phone } Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 6, 2012 Share Posted July 6, 2012 @jcbones, since the value is only numbers then there is no need to the capture expressions to only be numbers. You could just as easily use $pattern = '~(.{3})(.{3})(.{4})~'; However, in this case RegEx might be overkill since you can just use substr(). It might look like it's more complicated, but it's probably more efficient. Here's my two cents function formatPhone($p) { $phone = substr($p, 0, 3) . ' ' . substr($p, 3, 3) . ' ' . substr($p, 6); return $phone } You would think, but here is some benchmark'ing Array ( [0] => Array ( [Number_of_Loops_to_Perform_Test] => 10,000 [Number_of_Tests_Performed] => 3 [start_of_Test] => 07-06-2012 07:35:38pm ) [Test: 1] => Array ( [Code:] => $pattern = '~([0-9]{3})([0-9]{3})([0-9]{4})~'; $replacement = '($1)-$2-$3'; $number = 1112223333; preg_replace($pattern,$replacement,$number); [start] => 1341617738.0938 [End] => 1341617738.2981 [Elapsed] => 0.2043 ) [Test: 2] => Array ( [Code:] => $number = '1112223333'; if(!function_exists('formatPhone')) { function formatPhone($p) { $phone = substr($p, 0, 3) . ' ' . substr($p, 3, 3) . ' ' . substr($p, 6); return $phone; } } formatPhone($number); [start] => 1341617738.2981 [End] => 1341617738.7403 [Elapsed] => 0.44218 ) [Test: 3] => Array ( [Code:] => $number = '1112223333'; $phone = substr($number, 0, 3) . ' ' . substr($number, 3, 3) . ' ' . substr($number, 6); [start] => 1341617738.7403 [End] => 1341617738.979 [Elapsed] => 0.23869 ) ) Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 7, 2012 Share Posted July 7, 2012 I did say " . . . might be overkill". But, I put mine into a function which is good practice. And you added a function_exists() check which added even more overhead. When I reduced both methods down to the bare minimum my tests found the substr() solution to be faster. These were the lines I ran for the tests which are as apples to apples as I could get $value = preg_replace('~([0-9]{3})([0-9]{3})([0-9]{4})~', '($1)-$2-$3', $number); $value = substr($number, 0, 3) . '-' . substr($number, 3, 3) . '-' . substr($number, 6); Running each line 10,000 times with three runs each I got the following times: First method: Test 1: 0.025115966796875 Test 2: 0.025408029556274 Test 3: 0.025764942169189 Second method: Test 1: 0.022403955459595 Test 2: 0.022188186645508 Test 3: 0.022217035293579 Pwnd by .003 seconds! Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 7, 2012 Share Posted July 7, 2012 Yeah, I added a function exists because of my benchmark class, and how it works. That is why I ran the third test with just the string. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 7, 2012 Share Posted July 7, 2012 Not as fast but definitely more flexible, here's a little utility function of mine function formatIt($format,$str) { $i = $j = 0; $res = ''; $kf = strlen($format); $ks = strlen($str); while ($i < $kf && $j < $ks) { $res .= $format[$i]=='#' ? $str[$j++] : $format[$i]; ++$i; } if ($j<$ks) $res .= substr($str,$j); return $res; } $tel = '01234567891234'; echo formatIt ('(###)###-#### ext ####', $tel ); //--> (012)345-6789 ext 1234 ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted July 7, 2012 Share Posted July 7, 2012 You've triggered a pet peeve of mine so I'll make a quick point about something: I am storing phone numbers within the DB as a straight 10 digit int Phone numbers are not actually numbers. They are numeric but you can't, say, do math on them: 1112223333 + 1 is nonsensical, let alone something like division or modulus. It's blind luck that there aren't any 0XX or 00X area codes, otherwise that would screw things up. They are strings which consist of numbers. Store them as strings. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.