brandoncordell Posted July 13, 2009 Share Posted July 13, 2009 I'm a newb when it comes to PHP. I've been trying to learn for a year or two, but it's only been recently that I've been putting my mind to really learning. It's sometimes hard for me to understand concepts after just reading a tutorial or book, so I make "real world applications" like address books etc. Is there a better way to write this line. echo '<td>' . '(' . substr($row['number'], 0, 3) . ')' . substr($row['number'], 3, 3) . '-' . substr($row['number'], 6, 4) . '</td>'; (I'm trying to format a ten digit phone number into this format (555)123-1234 ) note: I'm learning MySQL techniques at the moment. I'm querying my database to pull a phone number (stored in a 10 character long VARCHAR, so just numbers 5551231234). Quote Link to comment https://forums.phpfreaks.com/topic/165852-newbie-to-php/ Share on other sites More sharing options...
RussellReal Posted July 13, 2009 Share Posted July 13, 2009 echo '('.substr($row['number'],0,3).')'.substr($row['number'],3,3).'-'.substr($row['number'],6,4); should work Quote Link to comment https://forums.phpfreaks.com/topic/165852-newbie-to-php/#findComment-874826 Share on other sites More sharing options...
kenrbnsn Posted July 13, 2009 Share Posted July 13, 2009 You can do something like this: <?php echo preg_replace("/([0-9a-zA-Z]{3})([0-9a-zA-Z]{3})([0-9a-zA-Z]{4})/", "($1)$2-$3", $row['number']); ?> I found this by doing a Google search... Ken Quote Link to comment https://forums.phpfreaks.com/topic/165852-newbie-to-php/#findComment-874827 Share on other sites More sharing options...
brandoncordell Posted July 13, 2009 Author Share Posted July 13, 2009 echo '('.substr($row['number'],0,3).')'.substr($row['number'],3,3).'-'.substr($row['number'],6,4); should work is there a better technique to retrieve certain parts of $row['number']? Reading through the PHP manual, substr seemed like the best, but then again I'm new Is this something regex could do for me? Quote Link to comment https://forums.phpfreaks.com/topic/165852-newbie-to-php/#findComment-874828 Share on other sites More sharing options...
RussellReal Posted July 13, 2009 Share Posted July 13, 2009 ken, what kinda regex is that if you're gonna go with regex use this: preg_replace("/^(\d{3})(\d{3})(\d{4})$/",'($1)$2-$3',$row['number']); --- Edit substr and strpos are really actually very fast I prefer using those two functions when parsing rather than regex, the above portion of this post is just to follow up on what ken said Quote Link to comment https://forums.phpfreaks.com/topic/165852-newbie-to-php/#findComment-874829 Share on other sites More sharing options...
kenrbnsn Posted July 13, 2009 Share Posted July 13, 2009 The line I posted was taken from this post: http://snipplr.com/view/3680/format-phone/ Ken Quote Link to comment https://forums.phpfreaks.com/topic/165852-newbie-to-php/#findComment-874830 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.