web_master Posted August 14, 2008 Share Posted August 14, 2008 hi, how can I split a phone number from dbase? <?php $query_return = mysql_query("SELECT * FROM `sms_txt` ORDER BY `sms_txt_id` DESC"); while($request = mysql_fetch_array($query_return)) { print $request['sms_txt_phonenr']; } ?> in this case I got this: 381631234567 (381 = country code, 6 = area code, 1234567 = phone nr) BUT I want to look like this (063) 123-4567 thnx in advanced! Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 <pre> <?php $num = '381631234567'; echo preg_replace('/\A\d{3}(\d{2})(\d{3})(\d{4})\z/e', 'sprintf("(%03d) %3d-%4d", $1, $2, $3)', $num); ?> </pre> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 14, 2008 Share Posted August 14, 2008 <pre> <?php $num = '381631234567'; echo preg_replace('/\A\d{3}(\d{2})(\d{3})(\d{4})\z/e', 'sprintf("(%03d) %3d-%4d", $1, $2, $3)', $num); ?> </pre> Absolutely elegent! You've done regular expressions before, haven't you? I can tell! Cheers, NRG Quote Link to comment Share on other sites More sharing options...
web_master Posted August 14, 2008 Author Share Posted August 14, 2008 nrg_alpha the effigy give me the solution before You Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 14, 2008 Share Posted August 14, 2008 nrg_alpha the effigy give me the solution before You I know that. I was complimenting him. Re-read my previous post carefully Quote Link to comment Share on other sites More sharing options...
web_master Posted August 14, 2008 Author Share Posted August 14, 2008 nrg, Im sorry - its because of my not a best english! best regards, T Quote Link to comment Share on other sites More sharing options...
Barand Posted August 14, 2008 Share Posted August 14, 2008 for the record, there is a non-regex method <?php $num = '381631234567'; list ($a, $b, $c, $d) = sscanf($num, '%3s%2s%3s%4s'); echo "(0$b) $c-$d"; ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 Beautiful Barand. I forgot about that gem. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 14, 2008 Share Posted August 14, 2008 I thought I'd post as CV moved it from PHP Help to Regex, so I assume he thought that was the only way Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 14, 2008 Share Posted August 14, 2008 for the record, there is a non-regex method <?php $num = '381631234567'; list ($a, $b, $c, $d) = sscanf($num, '%3s%2s%3s%4s'); echo "(0$b) $c-$d"; ?> Wow.. that is really efficient looking. But I am confused. I understand that the list is taking the first 3 characters and storing it as $a, the next two characters as $b, and so forth.. What I'm not sure about is the (0$b) part.. how does that manage to put $a in brackets? Quote Link to comment Share on other sites More sharing options...
effigy Posted August 14, 2008 Share Posted August 14, 2008 $a is the undesired "381," which is not used. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 14, 2008 Share Posted August 14, 2008 Oh right.. the first three digits is the country code (I had it in my head it was the area code). 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.