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
https://forums.phpfreaks.com/topic/165852-newbie-to-php/
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
https://forums.phpfreaks.com/topic/165852-newbie-to-php/#findComment-874828
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
https://forums.phpfreaks.com/topic/165852-newbie-to-php/#findComment-874829
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.