So let’s look at an image rotator that comes bundled with this WordPress theme. It’s actually coded by the founding WordPress developer and Internet ubergeek Matt Mullenweg (whose funky website is accompanied by a funky URL: http://ma.tt). The full code for the rotator is here, but I wanted to work my head around a couple of lines.
Here’s a snippet from the code:
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
Hmmmm, we need to look a bit closer at what’s going on there…
while (false !== ($file = readdir($handle))) {
A while loop, which will execute as long as the variable $file (which is assigned a filename from ‘readdir($handle)’ – where $handle is an resource created earlier on in the code from an opendir command) is not false (the ‘!==’ bit). In other words this loop will keep going as long as it can find files, and after each successful loop it will move onto the next file until there are no more.
foreach($exts as $ext) {
Earlier on in the code we assigned the variable $exts = ‘jpg jpeg png gif’. We then turned this string into an array of its component parts (separated by a space ‘ ‘) with $exts = explode(‘ ‘, $exts). So, on this second line we start to iterate through each item of the array $exts and call that item $ext.
if (preg_match('/.'.$ext.'$/i', $file, $test)) {
Oh good grief, here we go. That looks complicated. Let me rewrite a little clearer, assuming the first run-through where $ext is ‘jpg’.
if (preg_match('/ .jpg$ /i', $file, $test)) {
Let’s look at the middle bit,
'/ .jpg$ /i.'
The / at the beginning and end just signify the start and finish of the regular expression, so what we’re left with is .jpg$ and then the parameter ‘i’ which follows the second /. That ‘i’ parameter just means it’s case insensitive, so jpg will match with JPG for example.
The $ after .jpg$ just means to check that .jpg is at the end of a line which, for a file extension, it should be!
And, finally, the ‘.’ bit. All the backslash does is ‘escape’ (or ignore) the special character that comes after it. Without the PHP would try and do all kindsa crazy things with that period ‘.’ before the file extension jpg. In fact, that ‘.’ will match any single character except line break characters r and n without the backslash before it.
Okay, once we’ve worked out what we’re looking to match (basically those file extensions) we then match it to something (our $file variable) and pop the results in $test. And those results are, thanks to our while statment in line 1, a little bit irrelevant here. But it at least take us to the next line if the string is found…
$files[] = $file;
Okay, so we’ve found a file and we’re going to take it’s filename ($file) and pop it into an array we created earlier in the script ($files). Note the array is pluralised, just to make sure they are different!
++$i;
Finally we increment $i by one (the same as $i = $i + 1) and the loops continue. We use $i later in the script to get a random number from 0 to $i, and then grab the from the $files array the string of the file at $i position in the array. In other words, we are setting a top limit for the random number based on how many files there are in the directory.
I hope that makes some kind of sense. It is actually quite straightforward, but at the same time quite scary looking for the novice PHP’er. Ask me any questions and I’ll try and answer them.
If you’re still lost, it might make more sense here. For a more comprehensive breakdown of the code, check out Matt’s notes. Props also to Hudzilla for great explanations on regular expressions.