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.