scriptygoddess

02 Jul, 2002

Basic PHP tips, substr function

Posted by: Lynda In: How to's

I know I haven't been posting around these parts lately, but that doesn't mean I haven't had my hands dirty with some good old fashioned PHP. Most of what I've been doing is for pMachine (although I'd love to get into the new mySQL version of MT!) which is already documented on my site.

However, I come to you today to share my newest found and lately my favorite built in php function, substr

Basically what this does is tear apart a string and extract from it only what you want. This is useful for strings carefully built around dates or strings that combine several elements.

In pMachine for example, the archives string looks something like this:

id=D20020701

I know just from working with pMachine that the "D" denotes "daily", then the following are the year, month and day. If I wanted to use this information inside some code on the page, it would be virtually impossible without substr, as there's nothing separating each piece of information in order to explode it.

The basic composition of substr is this:

substr(string, start position, length);

Here's an example:

substr("abcdefg", 1, 2);
What we'd get from that would be "bc". Since 0 denotes the very first character in the string, "b" would be in "position 1" and it would then count two characters, including the starting one – bc.

To use it in the above pMachine archive example, we could completely dissect the "coded" id, using the following. Remember id=D20020701 :

$type = substr($id, 0, 1);
$year = substr($id, 1, 4);
$month = substr($id, 5, 2);
$day = substr($id, 7, 2);

Then, we can use each individual component of the string on our page.

Why is this useful?

I'm sure everyone can find hundreds of uses for this, but it seems to me it would be most useful for those who use php to display certain information on their site. Instead of having a url that looks like this:

http://www.yoursite.com/index.php?year=2002&month=07&day=01

you could just as easily put that all together into one string, which is more pleasing to the eye and MUCH easier to link to:

http://www.yoursite.com/index.php?date=20020701

Then you can dissect $date within the page so it will know what the $year, $month and $day are to display and do so accordingly.

2 Responses to "Basic PHP tips, substr function"

1 | Vix

July 2nd, 2002 at 10:07 pm

Avatar

Ooooh…I am going to play around with this. Thank you!!

2 | adrienne

July 12th, 2002 at 5:04 am

Avatar

Don't forget, you can also skip the third parameter. So, just:

substr("string",2) will return "ring" — without the length parameter, it will just go 'til the end of the string.

This turned out to just be useful for a pMachine hack for me — so i thought i'd share!

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements