<?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"
	>

<channel>
	<title>Talk In Code</title>
	<atom:link href="http://www.talkincode.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.talkincode.com</link>
	<description>Code tips, snippets and resources.</description>
	<pubDate>Fri, 10 Oct 2008 10:32:28 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Find The Number Of Days For A Given Month With PHP</title>
		<link>http://www.talkincode.com/find-the-number-of-days-for-a-given-month-with-php-557.html</link>
		<comments>http://www.talkincode.com/find-the-number-of-days-for-a-given-month-with-php-557.html#comments</comments>
		<pubDate>Fri, 10 Oct 2008 10:32:28 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[cal_days_in_month]]></category>

		<category><![CDATA[date]]></category>

		<category><![CDATA[days]]></category>

		<category><![CDATA[month]]></category>

		<category><![CDATA[number]]></category>

		<category><![CDATA[t]]></category>

		<category><![CDATA[year]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=557</guid>
		<description><![CDATA[There are two ways to find out the number of days for a given month.  The first is to use the date() function in conjunction with the mktime() function to create a date and format this value as the number of days in a given month.
$monthDays = date(&#34;t&#34;,mktime(0,0,0,12,1,2008));
The second way is to use the [...]]]></description>
			<content:encoded><![CDATA[<p>There are two ways to find out the number of days for a given month.  The first is to use the date() function in conjunction with the mktime() function to create a date and format this value as the number of days in a given month.</p>
<p><code class="php">$monthDays = date(&quot;t&quot;,mktime(0,0,0,12,1,2008));</code></p>
<p>The second way is to use the function cal_days_in_month().  This function takes three parameters.</p>
<ul>
<li><strong>Canelday</strong>: There are a number of different forms of calendar to chose from.  The one most English speaking people are interested in is the CAL_GREGORIAN calendar.  Take a look at a <a href="http://uk.php.net/manual/en/calendar.constants.php" title="">full list of calendar constands</a>.</li>
<li><strong>Month</strong>: An integer representing the month (1 - 12).</li>
<li><strong>Year</strong>: An integer representing the year.</li>
</ul>
<p>So to find out the number of days in the month of December of 2008 you could use the following.</p>
<p><code class="php">$monthDays = cal_days_in_month(CAL_GREGORIAN, 12, 2008);</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/find-the-number-of-days-for-a-given-month-with-php-557.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Find And Replace On All Files In And Below A Directory</title>
		<link>http://www.talkincode.com/find-and-replace-on-all-files-in-and-below-a-directory-554.html</link>
		<comments>http://www.talkincode.com/find-and-replace-on-all-files-in-and-below-a-directory-554.html#comments</comments>
		<pubDate>Thu, 09 Oct 2008 11:03:13 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[Unix/Linux]]></category>

		<category><![CDATA[find]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[replace]]></category>

		<category><![CDATA[sed]]></category>

		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=554</guid>
		<description><![CDATA[The following shell command uses the find function to find all files in or below the current directory that have the extension php.  It then passes each file found onto a sed command which then replaces all &#60;? with the longer &#038;lt?php version.
find . -name &#39;*.php&#39; -exec sed -ie &#39;s#&#60;?#&#60;?php#&#39; {} \;
The -name argument [...]]]></description>
			<content:encoded><![CDATA[<p>The following shell command uses the find function to find all files in or below the current directory that have the extension php.  It then passes each file found onto a sed command which then replaces all &lt;? with the longer &#038;lt?php version.</p>
<p><code>find . -name &#39;*.php&#39; -exec sed -ie &#39;s#&lt;?#&lt;?php#&#39; {} \;</code></p>
<p>The -name argument in find will look at the base of the file name, that is, the file without any directory path.  The -exec command is used to pass each file found onto another command, in this case sed  is used.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/find-and-replace-on-all-files-in-and-below-a-directory-554.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>PHP Function To Work Out Factorial Numbers</title>
		<link>http://www.talkincode.com/php-function-to-work-out-factorial-numbers-551.html</link>
		<comments>http://www.talkincode.com/php-function-to-work-out-factorial-numbers-551.html#comments</comments>
		<pubDate>Wed, 08 Oct 2008 08:58:28 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[calculate]]></category>

		<category><![CDATA[factorial]]></category>

		<category><![CDATA[function]]></category>

		<category><![CDATA[gmp_fact]]></category>

		<category><![CDATA[number]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=551</guid>
		<description><![CDATA[Factorial of a number is defined as the product of the number and all of the numbers small than it.  So if you take the number 4 the factorial of that number is 24 or 1 x 2 x 3 x 4.
Factorials are useful for a number of applications, for example when working out [...]]]></description>
			<content:encoded><![CDATA[<p>Factorial of a number is defined as the product of the number and all of the numbers small than it.  So if you take the number 4 the factorial of that number is 24 or 1 x 2 x 3 x 4.</p>
<p>Factorials are useful for a number of applications, for example when working out how many times a set of objects can be combined in different ways.</p>
<p>Use the following PHP function to work out the factorial of any given number.  The first thing it does it make sure that the number is greater than 1 because the factorial of the number 1 is 1.</p>
<p><code>function factorial($number){<br />
&nbsp;$result = 1;<br />
&nbsp;if($number &gt; 1){<br />
&nbsp;&nbsp;for($i = 2; $i &gt;= $number; $i++){<br />
&nbsp;&nbsp;&nbsp;$result *= $i;<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;return $result;<br />
}</code></p>
<p>The function works by using the *= operator, which does a calculation between the left and right sides (in this case multiplication) and stores the result on the left hand side.  This operator is much like the += operator, and works in the same way.</p>
<p>An alternative of this is the GMP function gmp_fact().  GMP is a set of maths functions that can be used to do some special things like add very large numbers together.  It has been part of the PHP core since version 4.0.4.</p>
<p>Beware that factorial numbers can get very large, very quickly.  For example, the factorial of the number 50 is 30,414,093,201,713,375,576,366,966,406,747,986,832,057,064,836,514,787,179,557,289,984.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/php-function-to-work-out-factorial-numbers-551.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Get Fibonacci Numbers Using PHP</title>
		<link>http://www.talkincode.com/get-fibonacci-numbers-using-php-547.html</link>
		<comments>http://www.talkincode.com/get-fibonacci-numbers-using-php-547.html#comments</comments>
		<pubDate>Tue, 07 Oct 2008 12:20:00 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[array]]></category>

		<category><![CDATA[calculate]]></category>

		<category><![CDATA[fibonacci]]></category>

		<category><![CDATA[number]]></category>

		<category><![CDATA[sequence]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=547</guid>
		<description><![CDATA[Fibonacci numbers not only have a few uses, but are also quite a nice little number sequence in themselves.
The sequence starts at 0, the next number is 1, and every number after that is the sum of the last two numbers.  So the third number is 1, and the second number is 2.
To create [...]]]></description>
			<content:encoded><![CDATA[<p>Fibonacci numbers not only have a few uses, but are also quite a nice little number sequence in themselves.</p>
<p>The sequence starts at 0, the next number is 1, and every number after that is the sum of the last two numbers.  So the third number is 1, and the second number is 2.</p>
<p>To create this number sequence in PHP we need to create the first two items in the array.  As we know that these are 0 and 1 we can create the array like this.</p>
<p><code class="php">$fibarray = array(0,1);</code></p>
<p>To create the third number we just add the first two numbers together.</p>
<p><code class="php">$fibarray[2] = $fibarray[0]+$fibarray[1];</code></p>
<p>This can be continued for as much as we want if we add it to a loop.</p>
<p><code class="php">for($i=0;$i&lt;=10;++$i){<br />
&nbsp;$fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];<br />
}</code></p>
<p>This will create an array with the following values.</p>
<p><code><br />
Array<br />
(<br />
&nbsp;[0] =&gt; 0<br />
&nbsp;[1] =&gt; 1<br />
&nbsp;[2] =&gt; 1<br />
&nbsp;[3] =&gt; 2<br />
&nbsp;[4] =&gt; 3<br />
&nbsp;[5] =&gt; 5<br />
&nbsp;[6] =&gt; 8<br />
&nbsp;[7] =&gt; 13<br />
&nbsp;[8] =&gt; 21<br />
&nbsp;[9] =&gt; 34<br />
&nbsp;[10] =&gt; 55<br />
)<br />
</code></p>
<p>Following on from this we can get a Fibonacci sequence to any position we want by using the following function.</p>
<p><code class="php">function fibonacciSequence($pos){<br />
&nbsp;$fibarray = array(0,1);<br />
&nbsp;for($i=0;$i&lt;=$pos;++$i){<br />
&nbsp;&nbsp;$fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];<br />
&nbsp;}<br />
&nbsp;return $fibarray;<br />
}</code></p>
<p>We can also create a very similar function that will only return a number at a particular position.</p>
<p><code class="php">function fibonacciSequence($pos){<br />
&nbsp;$fibarray = array(0,1);<br />
&nbsp;for($i=0;$i&lt;=$pos;++$i){<br />
&nbsp;&nbsp;$fibarray[$i] = $fibarray[$i-1] + $fibarray[$i-2];<br />
&nbsp;}<br />
&nbsp;return $fibarray[$pos];<br />
}</code></p>
<p>For example, if we pass this function the number 56, the result would be 225,851,433,717.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/get-fibonacci-numbers-using-php-547.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>PHP Script To Turn Image Into ASCII Text</title>
		<link>http://www.talkincode.com/php-script-to-turn-image-into-ascii-text-539.html</link>
		<comments>http://www.talkincode.com/php-script-to-turn-image-into-ascii-text-539.html#comments</comments>
		<pubDate>Mon, 06 Oct 2008 12:59:26 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[ascii]]></category>

		<category><![CDATA[convert]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[image]]></category>

		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=539</guid>
		<description><![CDATA[Use the following snippet to convert any jpeg image into the equivalent image in ASCII format.  It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it.  It then uses these values to loop through every pixel in the image and [...]]]></description>
			<content:encoded><![CDATA[<p>Use the following snippet to convert any jpeg image into the equivalent image in ASCII format.  It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it.  It then uses these values to loop through every pixel in the image and figures out the colour of that pixel.  It uses this value to create a &lt;span&gt; element that uses the text colour of a # to change the colour of the text.</p>
<p>An additional time (and space) saver for this function is that it detects any pixels that are just off white and simply displays a &amp;nbsp; character instead.</p>
<p><code class="php">$img = ImageCreateFromJpeg(&#39;logo.jpg&#39;);<br />
&nbsp;<br />
// get height and with of the image.<br />
$width = imagesx($img);<br />
$height = imagesy($img);<br />
&nbsp;<br />
echo &#39;&lt;pre style=&quot;font-size:1px;&quot;&gt;&#39;;<br />
for($h=0;$h&lt;$height;$h++){<br />
&nbsp;for($w=0;$w&lt;=$width;$w++){<br />
&nbsp;&nbsp;if($w == $width){<br />
&nbsp;&nbsp;&nbsp;echo &#39;&lt;br&gt;&#39;;<br />
&nbsp;&nbsp;&nbsp;continue;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;// get image at pixel location<br />
&nbsp;&nbsp;$rgb = ImageColorAt($img, $w, $h);<br />
&nbsp;&nbsp;// convert colour into usable format<br />
&nbsp;&nbsp;$r = ($rgb &gt;&gt; 16) &amp; 0xFF;<br />
&nbsp;&nbsp;$g = ($rgb &gt;&gt; 8 ) &amp; 0xFF;<br />
&nbsp;&nbsp;$b = $rgb &amp; 0xFF;<br />
&nbsp;&nbsp;// check for white/off-white colour<br />
&nbsp;&nbsp;if($r &gt; 200 &amp;&amp; $g &gt; 200 &amp;&amp; $b &gt; 200){<br />
&nbsp;&nbsp;&nbsp;echo &#39;&#038;nbsp&#39;;<br />
&nbsp;&nbsp;}else{<br />
&nbsp;&nbsp;&nbsp;echo &#39;&lt;span &nbsp;style=&quot;color:rgb(&#39;.$r.&#39;,&#39;.$g.&#39;,&#39;.$b.&#39;);&quot;&gt;#&lt;/span&gt;&#39;;<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
}<br />
echo &#39;&lt;/pre&gt;&#39;;</code></p>
<p>As an example, take the following, quite recognisable, image.<br />
<img src="http://www.talkincode.com/wp-content/uploads/2008/10/logo.jpg" alt="" title="Google Logo" width="276" height="110" class="alignnone size-full wp-image-543" /></p>
<p>This is transformed into the text on the following page.</p>
<p><a href="http://www.talkincode.com/examples/img-to-ascii/google-logo-ascii.html" title="The Google logo in ASCII format">The Google logo in ASCII format</a></p>
<p>Beware that although this works will for small image sizes if you try to convert a very large image you will find that the script takes a long time.  This is because it looks at every pixel in turn and converts the colour into something usable, so if there are a lot of pixels it takes a long time to look at every one.  Not only this, but you will also find that the size of the page generated will grow significantly due to all of the span elements being created.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/php-script-to-turn-image-into-ascii-text-539.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Planet PHP</title>
		<link>http://www.talkincode.com/planet-php-537.html</link>
		<comments>http://www.talkincode.com/planet-php-537.html#comments</comments>
		<pubDate>Thu, 02 Oct 2008 09:06:50 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP Websites]]></category>

		<category><![CDATA[aggregator]]></category>

		<category><![CDATA[blog]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[planet php]]></category>

		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=537</guid>
		<description><![CDATA[Planet PHP is a feed aggregator blog specifically for PHP related blogs.  There are a good number of blogs that the site uses to update with so there is always plenty of stuff going up on the site.  The site is not run by PHP, and is an independent project run by Christian [...]]]></description>
			<content:encoded><![CDATA[<p>Planet PHP is a feed aggregator blog specifically for PHP related blogs.  There are a good number of blogs that the site uses to update with so there is always plenty of stuff going up on the site.  The site is not run by PHP, and is an independent project run by Christian Stocker and Tobias Schlitt.</p>
<p><img src="http://www.talkincode.com/website-images/wwwplanet-phpnet.png" alt="Manage MySQL Databases With phpMyAdmin" /></p>
<p>The good thing about this site is that it saves having to go through all of the blogs in turn to see what is interesting in the world of PHP.  If you run a PHP blog you can submit your site to the list and get your articles included in the updates.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/planet-php-537.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Writing Function Code To Be More Readable</title>
		<link>http://www.talkincode.com/writing-function-code-to-be-more-readable-523.html</link>
		<comments>http://www.talkincode.com/writing-function-code-to-be-more-readable-523.html#comments</comments>
		<pubDate>Wed, 01 Oct 2008 08:30:36 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[better]]></category>

		<category><![CDATA[code]]></category>

		<category><![CDATA[elegant]]></category>

		<category><![CDATA[function]]></category>

		<category><![CDATA[if]]></category>

		<category><![CDATA[nested]]></category>

		<category><![CDATA[should]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=523</guid>
		<description><![CDATA[Last month I started writing functions in a particular way, which has made my life as a programmer much easier on more than one occasion.  No matter how many comments or verbose parameter names you put in you can end up writing code that you will get lost in.  The reason is simple. [...]]]></description>
			<content:encoded><![CDATA[<p>Last month I started writing functions in a particular way, which has made my life as a programmer much easier on more than one occasion.  No matter how many comments or verbose parameter names you put in you can end up writing code that you will get lost in.  The reason is simple.  Lets say you had a function that took in a couple of parameters.</p>
<p><code>function myFunction($intNum1,$intNum2){<br />
&nbsp;// function does something<br />
}</code></p>
<p>Normal practice is to check the parameters to make sure that they are what you expected them to be before continuing on with the rest of the function.  Lets say that we only want the numbers to be in a range.  If they are not in the range the function should return false.  Many programmers might start of writing something like this.</p>
<p><code>function myFunction($intNum1,$intNum2){<br />
&nbsp;if($intNum1 &gt; 0 &amp;&amp; $intNum1 &lt; 1000){<br />
&nbsp;&nbsp;if($intNum2 &gt; 0 &amp;&amp; $intNum2 &lt; 1000){<br />
&nbsp;&nbsp;&nbsp;// do something with the numbers.<br />
&nbsp;&nbsp;&nbsp;return $value;<br />
&nbsp;&nbsp;}else{<br />
&nbsp;&nbsp;&nbsp;return false;<br />
&nbsp;&nbsp;}<br />
&nbsp;}else{<br />
&nbsp;&nbsp;return false;<br />
&nbsp;}<br />
}</code></p>
<p>This code will still produce the desired effect of checking to see that the numbers are within a range, but there is a simpler and more elegant way of doing this.  For example, what if you wanted to add a third parameter, or a fourth?  Having nested if statements is hard enough, but when you get to four levels things get a bit silly.</p>
<p>Here is the same code, but refactored in a way that allows for better readability and maintenance.  Notice that the if statement logic has been reversed.</p>
<p><code>function myFunction($intNum1,$intNum2){<br />
&nbsp;// check parameter 1<br />
&nbsp;if($intNum1 &lt; 0 &amp;&amp; $intNum1 &gt; 1000){<br />
&nbsp;&nbsp;return false;<br />
&nbsp;}<br />
&nbsp;// check parameter 2<br />
&nbsp;if($intNum2 &lt; 0 &amp;&amp; $intNum2 &gt; 1000){<br />
&nbsp;&nbsp;return false;<br />
&nbsp;}<br />
&nbsp;// do something with the numbers.<br />
&nbsp;return $value;<br />
}</code></p>
<p>This way, we get past all of the logic checking before getting down to what we want the function to do, and it is readable.  Also, if you wanted to add another parameter you would just add another if statement to check that parameter.  There if no juggling of nested statements or complicated syntax.</p>
<p>The above function also doesn&#8217;t take into account the fact that the parameters might not be numbers, additional checking can be added at the top of the function to account for this.</p>
<p>I have posted this in the general category because I have used this technique in at least 3 different languages since I started doing it in PHP.  All of the code written here is in generic PHP like syntax, but the idea stands.  Hopefully, you should get something out of this as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/writing-function-code-to-be-more-readable-523.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Simple Swear Filter In PHP</title>
		<link>http://www.talkincode.com/simple-swear-filter-in-php-521.html</link>
		<comments>http://www.talkincode.com/simple-swear-filter-in-php-521.html#comments</comments>
		<pubDate>Tue, 30 Sep 2008 08:49:41 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP Strings]]></category>

		<category><![CDATA[e]]></category>

		<category><![CDATA[filter]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[preg_repalce]]></category>

		<category><![CDATA[stars]]></category>

		<category><![CDATA[swear]]></category>

		<category><![CDATA[words]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=521</guid>
		<description><![CDATA[Use the following function to filter out words from user input.  It works by having a pre-set array of words that are to be excluded, this array is then looped through and each item is used to replace any instances of that word within the text.  The regular expression uses the \b character [...]]]></description>
			<content:encoded><![CDATA[<p>Use the following function to filter out words from user input.  It works by having a pre-set array of words that are to be excluded, this array is then looped through and each item is used to replace any instances of that word within the text.  The regular expression uses the \b character class, which stands for any word boundary.  This way you don&#8217;t get the middle of words being filtered out when they are not meant to be.</p>
<p>By using the <strong>e</strong> of the preg_replace function it is possible to run PHP functions within the output.  In this case we count the number of characters found in the replace and use this to create a string of stars (*) of equal length.</p>
<p><code class="php">function filterwords($text){<br />
&nbsp;$filterWords = array(&#39;gosh&#39;,&#39;darn&#39;,&#39;poo&#39;);<br />
&nbsp;$filterCount = sizeof($filterWords);<br />
&nbsp;for($i=0; $i&lt;$filterCount; $i++){<br />
&nbsp;&nbsp;$text = preg_replace(&#39;/\b&#39;.$filterWords[$i].&#39;\b/ie&#39;,&quot;str_repeat(&#39;*&#39;,strlen(&#39;$0&#39;))&quot;,$text);<br />
&nbsp;}<br />
&nbsp;return $text;<br />
}</code></p>
<p>When the following text is run through this function.</p>
<p><code class="php">echo filterwords(&#39;Darn, I have a mild form of torretts, poo!&#39;);</code></p>
<p>It produces the following result.</p>
<p><code class="php">****, I have a mild form of torretts, ***!</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/simple-swear-filter-in-php-521.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Search Engine Spider Detection With PHP</title>
		<link>http://www.talkincode.com/search-engine-spider-detection-with-php-510.html</link>
		<comments>http://www.talkincode.com/search-engine-spider-detection-with-php-510.html#comments</comments>
		<pubDate>Mon, 29 Sep 2008 08:30:08 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[array]]></category>

		<category><![CDATA[detection]]></category>

		<category><![CDATA[engine]]></category>

		<category><![CDATA[search]]></category>

		<category><![CDATA[spiders]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=510</guid>
		<description><![CDATA[Part of any search engine optimisation strategy should always be that the user and the search engine see the same thing.  If you start delivering different content you will either end up not performing or just getting outright banned.  However, there are certain circumstances where you will want to detect the presence of [...]]]></description>
			<content:encoded><![CDATA[<p>Part of any search engine optimisation strategy should always be that the user and the search engine see the same thing.  If you start delivering different content you will either end up not performing or just getting outright banned.  However, there are certain circumstances where you will want to detect the presence of a search engine spider.  For example, lets say that you had a link to a section of your site, and you wanted to add a counter to it that registered an action every time a user clicked on the link.  One way of doing this would be to add a parameter to the URL of the link, if the parameter is present then it is a user going through the site and so the action will be registered.  You don&#8217;t want to register every time a search engine bot spiders the site so using the following function will allow you to turn off this parameter for these spiders.</p>
<p><code class="php">function spiderDetect() {<br />
&nbsp;$agentArray = array(&quot;ArchitextSpider&quot;, &quot;Googlebot&quot;, &quot;TeomaAgent&quot;,<br />
&nbsp;&nbsp;&quot;Zyborg&quot;, &quot;Gulliver&quot;, &quot;Architext spider&quot;, &quot;FAST-WebCrawler&quot;,<br />
&nbsp;&nbsp;&quot;Slurp&quot;, &quot;Ask Jeeves&quot;, &quot;ia_archiver&quot;, &quot;Scooter&quot;, &quot;Mercator&quot;,<br />
&nbsp;&nbsp;&quot;crawler@fast&quot;, &quot;Crawler&quot;, &quot;InfoSeek Sidewinder&quot;,<br />
&nbsp;&nbsp;&quot;almaden.ibm.com&quot;, &quot;appie 1.1&quot;, &quot;augurfind&quot;, &quot;baiduspider&quot;,<br />
&nbsp;&nbsp;&quot;bannana_bot&quot;, &quot;bdcindexer&quot;, &quot;docomo&quot;, &quot;frooglebot&quot;, &quot;geobot&quot;,<br />
&nbsp;&nbsp;&quot;henrythemiragorobot&quot;, &quot;sidewinder&quot;, &quot;lachesis&quot;, &quot;moget/1.0&quot;,<br />
&nbsp;&nbsp;&quot;nationaldirectory-webspider&quot;, &quot;naverrobot&quot;, &quot;ncsa beta&quot;,<br />
&nbsp;&nbsp;&quot;netresearchserver&quot;, &quot;ng/1.0&quot;, &quot;osis-project&quot;, &quot;polybot&quot;,<br />
&nbsp;&nbsp;&quot;pompos&quot;, &quot;seventwentyfour&quot;, &quot;steeler/1.3&quot;, &quot;szukacz&quot;,<br />
&nbsp;&nbsp;&quot;teoma&quot;, &quot;turnitinbot&quot;, &quot;vagabondo&quot;, &quot;zao/0&quot;, &quot;zyborg/1.0&quot;,<br />
&nbsp;&nbsp;&quot;Lycos_Spider_(T-Rex)&quot;, &quot;Lycos_Spider_Beta2(T-Rex)&quot;,<br />
&nbsp;&nbsp;&quot;Fluffy the Spider&quot;, &quot;Ultraseek&quot;, &quot;MantraAgent&quot;,&quot;Moget&quot;,<br />
&nbsp;&nbsp;&quot;T-H-U-N-D-E-R-S-T-O-N-E&quot;, &quot;MuscatFerret&quot;, &quot;VoilaBot&quot;,<br />
&nbsp;&nbsp;&quot;Sleek Spider&quot;, &quot;KIT_Fireball&quot;, &quot;WISEnut&quot;, &quot;WebCrawler&quot;,<br />
&nbsp;&nbsp;&quot;asterias2.0&quot;, &quot;suchtop-bot&quot;, &quot;YahooSeeker&quot;, &quot;ai_archiver&quot;,<br />
&nbsp;&nbsp;&quot;Jetbot&quot;<br />
&nbsp;);<br />
&nbsp;<br />
&nbsp;$theAgent = $_SERVER[&quot;HTTP_USER_AGENT&quot;];<br />
&nbsp;<br />
&nbsp;for($i=0;$i&lt;count($agentArray);$i++){<br />
&nbsp;&nbsp;if(strpos(&quot; &quot;.strtolower($theAgent), strtolower($agentArray[$f]))!= false){<br />
&nbsp;&nbsp;&nbsp;return true;<br />
&nbsp;&nbsp;};<br />
&nbsp;};<br />
&nbsp;return false;<br />
}</code></p>
<p>The function works by finding the current user agent string of the visitor and the comparing it to the list of user agents in an array.  If the user agent is found then true is returned, otherwise the return value is false.</p>
<p>With this function present you can now include an if statement to see if the user agent is a search engine spider or not.</p>
<p><code class="php">if(spiderDetect()){<br />
&nbsp;// do something for spiders<br />
}else{<br />
&nbsp;// do something for users<br />
}</code></p>
<p>Please be careful with this function.  If you server different content to users and search engine spiders you will more than likely get banned for your efforts.</p>
<p>Also, this might be an incomplete list of search engine spider user agents, if you know any more then please write a comment and I will add them onto this list.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/search-engine-spider-detection-with-php-510.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Directory Iteration With DirectoryIterator() In PHP 5</title>
		<link>http://www.talkincode.com/directory-iteration-with-directoryiterator-in-php-5-518.html</link>
		<comments>http://www.talkincode.com/directory-iteration-with-directoryiterator-in-php-5-518.html#comments</comments>
		<pubDate>Fri, 26 Sep 2008 09:53:16 +0000</pubDate>
		<dc:creator>Tech</dc:creator>
		
		<category><![CDATA[PHP OOP]]></category>

		<category><![CDATA[current]]></category>

		<category><![CDATA[directory]]></category>

		<category><![CDATA[DirectoryIterator]]></category>

		<category><![CDATA[getcwd]]></category>

		<category><![CDATA[iterator]]></category>

		<category><![CDATA[next]]></category>

		<guid isPermaLink="false">http://www.talkincode.com/?p=518</guid>
		<description><![CDATA[The normal way of looping through a directory is to get a handle on the directory, then go through each item, making sure that the file is readable and is not &#34;.&#34; or &#34;..&#34; before doing something with the file.  Because this is done a lot the DirectoryIterator() object was created in PHP5 to [...]]]></description>
			<content:encoded><![CDATA[<p>The normal way of looping through a directory is to get a handle on the directory, then go through each item, making sure that the file is readable and is not &quot;.&quot; or &quot;..&quot; before doing something with the file.  Because this is done a lot the DirectoryIterator() object was created in PHP5 to simplify this process.</p>
<p>The constructor of the DirectoryIterator() object takes a single parameter, this is the path of the directory that is to be iterated over.</p>
<p><code class="php">$dir = new DirectoryIterator(&#39;/&#39;);</code></p>
<p>To get the current working directory you can use the PHP function getcwd().  This will return the directory that the PHP script is being run from.</p>
<p><code class="php">$dir = new DirectoryIterator(getcwd());</code></p>
<p>The previous bit of code will open up the root directory of your server.  If you are on a Linux server you will see directories like dev, var, srv, etc, lib, home, root, sbin and usr.</p>
<p>With the object now created there are some different functions that can be used to look at different things.  Here is a small list of what can be used:</p>
<ul>
<li><strong>isDot()</strong>: This function is used to see if the file is a &quot;.&quot; or a &quot;..&quot;.</li>
<li><strong>isReadable ()</strong>: Checks if the file is readable.  If it is then it returns true, otherwise it is false.</li>
<li><strong>next()</strong>: Moves the iterator onto the next item in the list.</li>
<li><strong>valid()</strong>: This function checks to see if there is anything in the directory that can be iterated over.  Basically, this function returns false if the iterator is at the end of the directory.</li>
<li><strong>current()</strong>: Returns the name of the current file.</li>
</ul>
<p>These functions can be put together in the following way.  This snippet of code will print out all of the files in a directory.</p>
<p><code class="php">while($dir-&gt;valid()){<br />
&nbsp;if(!$dir-&gt;isDot()){<br />
&nbsp;&nbsp;print $dir->current().&quot;&lt;br /&gt;&quot;;<br />
&nbsp;}<br />
&nbsp;$dir-&gt;next();<br />
}</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.talkincode.com/directory-iteration-with-directoryiterator-in-php-5-518.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
