<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Richard Shepherd &#187; image rotator</title>
	<atom:link href="http://www.richardshepherd.com/tag/image-rotator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardshepherd.com</link>
	<description>Making the web. Better looking.</description>
	<lastBuildDate>Fri, 04 Jun 2010 18:03:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>php Image Rotator :: Making sense of the code</title>
		<link>http://www.richardshepherd.com/php-image-rotator-making-sense-of-the-code/</link>
		<comments>http://www.richardshepherd.com/php-image-rotator-making-sense-of-the-code/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 09:41:15 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[image rotator]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=147</guid>
		<description><![CDATA[So let&#8217;s look at an image rotator that comes bundled with this WordPress theme. It&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>So let&#8217;s look at an image rotator that comes bundled with this WordPress theme. It&#8217;s actually coded by the founding WordPress developer and Internet ubergeek <a href="http://ma.tt/ " target="_blank">Matt Mullenweg</a> (whose funky website is accompanied by a funky URL: <a href="http://ma.tt">http://ma.tt</a>). <a href="http://ma.tt/scripts/randomimage/" target="_blank">The full code for the rotator is here</a>, but I wanted to work my head around a couple of lines.</p>
<p>Here&#8217;s a snippet from the code:</p>
<pre name="code" class="php">
 while (false !== ($file = readdir($handle))) {
    foreach($exts as $ext) {
       if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
          $files[] = $file;
          ++$i;
       }
    }
 }</pre>
<p>Hmmmm, we need to look a bit closer at what&#8217;s going on there&#8230; <span id="more-147"></span></p>
<pre name="code" class="php"> while (false !== ($file = readdir($handle))) {</pre>
<p>A while loop, which will execute as long as the variable $file (which is assigned a filename from &#8216;readdir($handle)&#8217; &#8211; where $handle is an <a href="http://uk3.php.net/manual/en/language.types.resource.php" target="_blank">resource</a> created earlier on in the code from an <a href="http://uk2.php.net/opendir" target="_blank">opendir</a> command) is not false (the &#8216;!==&#8217; 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.</p>
<pre name="code" class="php">
 foreach($exts as $ext) { </pre>
<p>Earlier on in the code we assigned the variable $exts = &#8216;jpg jpeg png gif&#8217;. We then turned this string into an array of its component parts (separated by a space &#8216; &#8216;) with $exts = explode(&#8216; &#8216;, $exts).  So, on this second line we start to iterate through each item of the array $exts and call that item $ext.</p>
<pre name="code" class="php">
       if (preg_match('/\.'.$ext.'$/i', $file, $test)) { </pre>
<p>Oh good grief, here we go. That looks complicated. Let me rewrite a little clearer, assuming the first run-through where $ext is &#8216;jpg&#8217;.</p>
<pre name="code" class="php">
       if (preg_match('/ \.jpg$ /i', $file, $test)) { </pre>
<p>Let&#8217;s look at the middle bit,
<pre name="code" class="php">
'/ \.jpg$ /i.' </pre>
<p>The / at the beginning and end just signify the start and finish of the <a href="http://www.regular-expressions.info/php.html" target="_blank">regular expression</a>, so what we&#8217;re left with is \.jpg$ and then the parameter &#8216;i&#8217; which follows the second /. That &#8216;i&#8217; parameter just means it&#8217;s case <em>insensitive</em>, so jpg will match with JPG for example.</p>
<p>The $ after .jpg$ just means to check that .jpg is at the end of a line which, for a file extension, it should be!</p>
<p>And, finally, the &#8216;\.&#8217; bit. All the backslash \ does is &#8216;escape&#8217; (or ignore) the special character that comes after it. Without the \ PHP would try and do all kindsa crazy things with that period &#8216;.&#8217; before the file extension jpg. In fact, that &#8216;.&#8217; will match any single character except line break characters \r and \n without the backslash before it.</p>
<p>Okay, once we&#8217;ve worked out what we&#8217;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&#8230;</p>
<pre name="code" class="php">
          $files[] = $file; </pre>
<p>Okay, so we&#8217;ve found a file and we&#8217;re going to take it&#8217;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!</p>
<pre name="code" class="php">
          ++$i;</pre>
<p>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.</p>
<p>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&#8217;er. Ask me any questions and I&#8217;ll try and answer them.</p>
<p>If you&#8217;re still lost, it might make more sense <a href="http://hudzilla.org/phpwiki/index.php?title=Basic_string_matching_with_regular_expressions" target="_blank">here</a>. For a more comprehensive breakdown of the code, check out <a href="http://ma.tt/2003/05/a-better-image-rotator/" target="_blank">Matt&#8217;s notes</a>. Props also to <a href="http://www.hudzilla.org/phpbook/read.php/4_8_1" target="_blank">Hudzilla</a> for great explanations on <a href="http://hudzilla.org/phpwiki/index.php?title=Regular_expressions" target="_blank">regular expressions</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/php-image-rotator-making-sense-of-the-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>All Change, please. All Change.</title>
		<link>http://www.richardshepherd.com/all-change-please-all-change/</link>
		<comments>http://www.richardshepherd.com/all-change-please-all-change/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 11:24:32 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[image rotator]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=106</guid>
		<description><![CDATA[So, we&#8217;re back. It&#8217;s been almost a year but it&#8217;s time to get this show on the road once more. What can we expect from RichardShepherd.com? More and more updates, correctly categorised and tagged for your viewing pleasure. See that bunch of random words in the top right corner? Well, as I add more and [...]]]></description>
			<content:encoded><![CDATA[<p>So, we&#8217;re back. It&#8217;s been almost a year but it&#8217;s time to get this show on the road once more.</p>
<p>What can we expect from RichardShepherd.com? More and more updates, correctly categorised and tagged for your viewing pleasure. See that bunch of random words in the top right corner? Well, as I add more and more info those words will start to inflate. The larger the word, the more I&#8217;ve written about it. It&#8217;s called a Tag Cloud, and it&#8217;s the future, man.</p>
<p>I&#8217;ll be writing, blogging if you will, three different things over the coming months..</p>
<p>The first is web design. I&#8217;m what you might call an intermediate user, and I want to take this to advanced. This involves brushing up on php, mySQL and Javascript in particular. I&#8217;ll be embarking on a few projects, like designing a WordPress widget and Facebook application from scratch, and hopefully we&#8217;ll all learn something along the way. Of course as I do, this site will evolve and be a work in progress. A playground, if you will.</p>
<p>The second is film. Or film-making, to be precise. I&#8217;ve had a modicum of success with some short films and commercials I&#8217;ve directed, but it&#8217;s been over a year since I called &#8216;action!&#8217; so it&#8217;s time to get our hands dirty again. As we piece together our next epic short, the front row seats to the behind the scenes action is right here.</p>
<p>The third is the ramblings from my over-productive and underutilised brain. We have a lot to discuss, and that why there&#8217;s a new poll feature on the right hand side of these posts. I&#8217;ll change it every so often, and would love to hear ideas or questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/all-change-please-all-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
