Jump to content

Newbie to PHP


brandoncordell

Recommended Posts

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).

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

ken, what kinda regex is that :P

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.