<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Cowburn</title>
	
	<link>http://cowburn.info</link>
	<description>Online storage depot for Peter Cowburn</description>
	<lastBuildDate>Thu, 22 Jul 2010 17:51:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/cowburn" /><feedburner:info uri="cowburn" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><meta xmlns="http://pipes.yahoo.com" name="pipes" content="noprocess" /><item>
		<title>PHP 5.3.3 released!</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/8veSQFx0NxM/</link>
		<comments>http://cowburn.info/2010/07/22/php-5-3-3-released/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 10:47:14 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php.net]]></category>
		<category><![CDATA[php5.3.3]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=440</guid>
		<description><![CDATA[Today, the latest version of PHP 5.3 (as well as the latest in the 5.2 branch, 5.2.14) has been released to the masses. The official announcement can found in the news archive with finer details in the changelog. A select few of the changes, of particular interest to me, are outlined below. Added support for [...]]]></description>
			<content:encoded><![CDATA[<p>Today, the latest version of PHP 5.3 (as well as the latest in the 5.2 branch, 5.2.14) has been released to the masses. The official announcement can found in the <a href="http://www.php.net/archive/2010.php#id2010-07-22-2" title="PHP: News Archive - 2010">news archive</a> with finer details in the <a href="http://www.php.net/ChangeLog-5.php#5.3.3" title="PHP 5.3.3 Changelog">changelog</a>.  A select few of the changes, of particular interest to me, are outlined below.<span id="more-440"></span></p>
<blockquote cite="http://www.php.net/ChangeLog-5.php#5.3.3">
<ul>
<li>Added support for copy to/from array/file for pdo_pgsql extension. (Denis Gasparin, Ilia)</li>
<li>Changed namespaced classes so that the ctor can only be named __construct now. (Stas)</li>
<li>Fixed bug #51590 (JSON_ERROR_UTF8 is undefined). (Felipe)</li>
<li>Fixed bug #49576 (FILTER_VALIDATE_EMAIL filter needs updating) (Rasmus)</li>
<li>Fixed bug #48361 (SplFileInfo::getPathInfo should return the parent dir). (Etienne)</li>
<li>And well over a hundred other bug and security fixes</li>
</ul>
</blockquote>
<p>The change to constructors in namespaced classes has already been the subject of &#8220;why doesn&#8217;t this work&#8221; questions. With 5.3.3 you <em>cannot</em> define class constructors like below.</p>
<pre><code lang="php">
namespace Foo {
    class Bar {
        function Bar() {
            // constructing \Foo\Bar class
        }
    }
}
</code></pre>
<p>Instead, you need to be using the right and proper <code lang="php">__construct</code> method. Note that this only applies to namespaced classes, so old code can continue (if they like) using the class-name-based constructor. This really only affects the (few?) folks who might be making use of, or adapting their old code to use, namespaces yet still keeping the old style of naming the constructor method.</p>
<p>Go, play!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=8veSQFx0NxM:FOhIN46HdjY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=8veSQFx0NxM:FOhIN46HdjY:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/8veSQFx0NxM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/07/22/php-5-3-3-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2010/07/22/php-5-3-3-released/</feedburner:origLink></item>
		<item>
		<title>Happy 15th, PHP</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/Cs_-f5_GIk0/</link>
		<comments>http://cowburn.info/2010/06/08/happy-15th-php/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 07:58:57 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[birthday]]></category>
		<category><![CDATA[php.net]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=429</guid>
		<description><![CDATA[Today marks the 15th birthday* of PHP. I did have a nice, long post planned but lost it during the drafting process (maybe I should use WordPress which auto-saves?). So, in a new concise train of thought I&#8217;ll leave the rambling to other folks in the PHP community who will (hopefully) be providing their reflections [...]]]></description>
			<content:encoded><![CDATA[<p>Today marks the 15th birthday<a href="#fn1">*</a> of PHP. I did have a nice, long post planned but lost it during the drafting process (maybe I should use WordPress which auto-saves?).  So, in a new concise train of thought I&#8217;ll leave the rambling to other folks in the PHP community who will (hopefully) be providing their reflections over the last decade and a half. I&#8217;ll amend links to this post for those who don&#8217;t want to search around for birthday wishes.</p>
<p>Happy birthday, PHP! <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <span id="more-429"></span></p>
<p class="aside" id="fn1">* See <a href="http://groups.google.com/group/comp.infosystems.www.authoring.cgi/browse_thread/thread/3f2276e6067acfb0/cc7d43454d64d133" title="Announce: Personal Home Page Tools (PHP Tools) Options">Announce: Personal Home Page Tools (PHP Tools) Options</a> (8th June 1995)</p>
<h3>Elsewhere</h3>
<ul>
<li><a href="http://schlueters.de/blog/archives/136-15-years-of-PHP.html" title="15 years of PHP">15 years of PHP</a> from Johannes Schlüter</li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=Cs_-f5_GIk0:UBkbYBbjmAI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=Cs_-f5_GIk0:UBkbYBbjmAI:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/Cs_-f5_GIk0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/06/08/happy-15th-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2010/06/08/happy-15th-php/</feedburner:origLink></item>
		<item>
		<title>Glob Patterns for File Matching in PHP</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/8wFCszIwzU0/</link>
		<comments>http://cowburn.info/2010/04/30/glob-patterns/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 18:16:46 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[glob]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=390</guid>
		<description><![CDATA[This article has been on the cards for a while now with recent articles elsewhere1,2 prompting me to get this finished and up on the blog. The focus here is on the wildcard patterns that can be used with glob. Intro to the glob function To quote the succinct description on the PHP manual page [...]]]></description>
			<content:encoded><![CDATA[<p>This article has been on the cards for a while now with recent articles elsewhere<sup><a href="#fn1" title="php|architect - Putting glob() to the test">1</a>,<a href="#fn2" title="Nettuts+ - Loop Through Folders with PHP’s Glob()">2</a></sup> prompting me to get this finished and up on the blog. The focus here is on the wildcard patterns that can be used with <code>glob</code>.<span id="more-390"></span>  </p>
<h3>Intro to the <code>glob</code> function</h3>
<p>To quote the succinct description on the <a href="http://php.net/glob" title="PHP: Glob">PHP manual page</a> for <code>glob</code>:</p>
<blockquote cite="http://php.net/glob" datetime="2010-04-30T16:20:00Z"><p>The <code>glob()</code> function searches for all the pathnames matching <var>pattern</var> according to the rules used by the libc <code>glob()</code> function, which is similar to the rules used by common shells.<br />
<cite>http://php.net/glob</cite></p></blockquote>
<h3>What&#8217;s in a pattern?</h3>
<p>Most people who have already encountered <code>glob</code> know to make use of the <code>*</code> metacharacter to match <em>some characters</em>, and those digging a little deeper often discover that discrete alternatives can be globbed with braces (e.g. <code>image.{gif,jpg,png}</code>). However, there are more special characters and sequences that can be used to be more (or less, if we want) specific about what to find. </p>
<p class="small"><em>Aside</em>: please <strong>do not</strong> make the mistake of thinking that glob patterns are <em>regular expressions</em>, they&#8217;re just not. If you do want to use regular expressions to find paths/files then you are invited to use SPL&#8217;s <a href="http://php.net/regexiterator" title="PHP: SPL RegexIterator">RegexIterator</a>, which allows filtering of an Iterator based on a PCRE regex, in conjunction with a DirectoryIterator or FilesystemIterator (there are recursive flavours of the Regex- and DirectoryIterator if you need to delve into folders). For those SPL-ly inclined, also note the <a href="http://php.net/globiterator" title="PHP: SPL GlobIterator">GlobIterator</a> which combines the goodness of globbing with iteration. If that made entirely <em>no sense</em>, please read on! Globs are much less verbose…</p>
<p>So, here are the special doohickeys (technical term!) that we can use with <code>glob</code>:</p>
<dl>
<dt><code>*</code> (an asterisk)</dt>
<dd>Matches zero of more characters.</dd>
<dt><code>?</code></dt>
<dd>Matches exactly any one character.</dd>
<dt><code>[...]</code></dt>
<dd>Matches one character from a group.  A group can be a list of characters, e.g. <code>[afkp]</code>, or a range of characters, e.g. <code>[a-g]</code> which is the same as <code>[abcdefg]</code>.</dd>
<dt><code>[!...]</code></dt>
<dd>Matches any single character not in the group. <code>[!a-zA-Z0-9]</code> matches any character that is not alphanumeric.</dd>
<dt><code>\</code></dt>
<dd>Escapes the next character. For special characters, this causes them to not be treated as special. For example, <code>\[</code> matches a literal <samp>[</samp>. If <var>flags</var> includes <var>GLOB_NOESCAPE</var>, this quoting is disabled and <code>\</code> is handled as a simple character. </dd>
</dl>
<h3>Globbingly good glob examples</h3>
<p>Here are a few examples of what globs might look like alongside a brief description of the intended behaviour: if you have any suggestions please do make them in the comments as I'm running short on inspiration! </p>
<table class="light">
<thead>
<tr style="width:20%">
<th>pattern</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>*.txt</code></td>
<td>Get directory contents which have the extension of <samp>.txt</samp> (Note: a file could be named simply <samp>.txt</samp>!).</td>
</tr>
<tr>
<td><code>??</code></td>
<td>Get directory contents with names <em>exactly</em> two characters in length.</td>
</tr>
<tr>
<td><code>??*</code></td>
<td>Get directory contents with names <em>at least</em> two characters in length.</td>
</tr>
<tr>
<td><code>g?*</code></td>
<td>Get directory contents with names at least two characters in length and starting with the letter <samp>g</samp></td>
</tr>
<tr>
<td><code>*.{jpg,gif,png}</code></td>
<td>Get directory contents with an extension of <samp>.jpg</samp>, <samp>.gif</samp> or <samp>.png</samp>. Remember to use the <var>GLOB_BRACE</var> flag.</td>
</tr>
<tr>
<td><code>DN?????.dat</code></td>
<td>Get directory contents which start with the letters <samp>DN</samp>, followed by five characters, with an extension of <samp>.dat</samp>.</td>
</tr>
<tr>
<td><code>DN[0-9][0-9][0-9][0-9][0-9].dat</code></td>
<td>Get directory contents which start with the letters <samp>DN</samp>, followed by five <em>digits</em>, with an extension of <samp>.dat</samp>.</td>
</tr>
<tr>
<td><code>[!aeiou]*</code></td>
<td>Get directory contents which do not start with a vowel letter.</td>
</tr>
<tr>
<td><code>[!a-d]*</code></td>
<td>Get directory contents which do not start with <samp>a</samp>, <samp>b</samp>, <samp>c</samp> or <samp>d</samp>.</td>
</tr>
<tr>
<td><code>*\[[0-9]\].*</code></td>
<td>Get directory contents whose basename ends with a single digit enclosed in square braces. If <var>GLOB_NOESCAPE</var> is used, a single digit enclosed in <samp>\[</samp> and <samp>\]</samp> which would be a pretty weird name.</td>
</tr>
<tr>
<td><code>subdir/img*/th_?*</code></td>
<td>Get directory contents whose name starts with <samp>th_</samp> (with at least one character after that) within directories whose names start with <samp>img</samp> in the <samp>subdir</samp> directory.</td>
</tr>
</tbody>
</table>
<p>Well there we go, I&#8217;ve said what I came here to say so all that remains to be done is give some link love to those  two recent articles that prompted me to dust off this draft and click the &#8220;publish&#8221; button. </p>
<h6>With thanks</h6>
<ol>
<li id="fn1">php|architect&#8217;s <a href="http://www.phparch.com/2010/04/28/putting-glob-to-the-test/" title="Putting glob() to the test">Putting glob() to the test</a> focuses on execution times of <code>glob</code> and other techniques of getting a list of files.</li>
<li id="fn2">Marcus Schumann&#8217;s <a href="http://net.tutsplus.com/tutorials/php/quick-tip-loop-through-folders-with-phps-glob/" title="Loop Through Folders with PHP’s Glob()">Loop Through Folders with PHP’s Glob()</a> article on Nettuts+ gives a brief introduction for those who might not have been introduced to <code>glob</code>.</li>
</ol>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=8wFCszIwzU0:-pBDMfJ4uHw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=8wFCszIwzU0:-pBDMfJ4uHw:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/8wFCszIwzU0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/04/30/glob-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2010/04/30/glob-patterns/</feedburner:origLink></item>
		<item>
		<title>PHP 5.3.2 Released</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/CGc6MlGtpi0/</link>
		<comments>http://cowburn.info/2010/03/04/php-5-3-2-released/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 21:51:49 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php.net]]></category>
		<category><![CDATA[php5.3.2]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=387</guid>
		<description><![CDATA[Today, the latest version of PHP 5.3 has been released to the masses. The official announcement can found in the news archive with finer details in the changelog. Key points were outlined in the announcement, partially quoted below.]]></description>
			<content:encoded><![CDATA[<p>Today, the latest version of PHP 5.3 has been released to the masses. The official announcement can found in the <a href="http://www.php.net/archive/2010.php#id2010-03-04-1" title="PHP: News Archive - 2010">news archive</a> with finer details in the <a href="http://www.php.net/ChangeLog-5.php#5.3.2" title="PHP 5.3.2 Changelog">changelog</a>.  Key points were outlined in the announcement, partially quoted below.<span id="more-387"></span></p>
<blockquote cite=""http://www.php.net/archive/2010.php#id2010-03-04-1">
<h4>Security Enhancements and Fixes in PHP 5.3.2:</h4>
<ul>
<li>Improved LCG entropy. (Rasmus, Samy Kamkar)</li>
<li>Fixed safe_mode validation inside tempnam() when the directory path does not end with a /). (Martin Jansen)</li>
<li>Fixed a possible open_basedir/safe_mode bypass in the session extension identified by Grzegorz Stachowiak. (Ilia)</li>
</ul>
<h4>Key Bug Fixes in PHP 5.3.2 include:</h4>
<ul>
<li>Added support for SHA-256 and SHA-512 to php&#8217;s crypt.</li>
<li>Added protection for $_SESSION from interrupt corruption and improved &#8220;session.save_path&#8221; check.</li>
<li>Fixed bug #51059 (crypt crashes when invalid salt are given).</li>
<li>Fixed bug #50940 Custom content-length set incorrectly in Apache sapis.</li>
<li>Fixed bug #50847 (strip_tags() removes all tags greater then 1023 bytes long).</li>
<li>Fixed bug #50723 (Bug in garbage collector causes crash).</li>
<li>Fixed bug #50661 (DOMDocument::loadXML does not allow UTF-16).</li>
<li>Fixed bug #50632 (filter_input() does not return default value if the variable does not exist).</li>
<li>Fixed bug #50540 (Crash while running ldap_next_reference test cases).</li>
<li>Fixed bug #49851 (http wrapper breaks on 1024 char long headers).</li>
<li>Over 60 other bug fixes.</li>
</ul>
</blockquote>
<p>Other nice improvements with this version include an (almost) up-to-date <a href="http://pcre.org" title="PCRE - Perl Compatible Regular Expressions">PCRE</a> library (PHP 5.3.2 includes PCRE 8.00 with the latest version being 8.01) for regular expressions and a number of nice improvements to the <a href="http://php.net/filter" title="PHP: Filter - Manual">filter extension</a> both of which I use very regularly.  </p>
<p>Do you, dear reader, keep up-to-date with PHP 5.3 (or 5.2) or are you stuck with steadily aging versions due to restrictive hosting providers?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=CGc6MlGtpi0:EO66ShuAClU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=CGc6MlGtpi0:EO66ShuAClU:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/CGc6MlGtpi0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/03/04/php-5-3-2-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2010/03/04/php-5-3-2-released/</feedburner:origLink></item>
		<item>
		<title>Using PHP Functions in XPath Expressions</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/WojNZ1aU-30/</link>
		<comments>http://cowburn.info/2009/10/23/php-funcs-xpath/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 20:41:13 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XPath]]></category>
		<category><![CDATA[DOM]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=336</guid>
		<description><![CDATA[Disclaimer: this article expects familiarity with using the DOM1 extension and XPath2 expressions. The (currently undocumentednow documented3) DOMXPath::registerPHPFunctions method is available as of PHP 5.3.0 (it was added to the code base back in December 2006) and allows the use of PHP functions (and static methods) within XPath queries to complement the normal set of [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: this article expects familiarity with using the DOM<sup><a href="#fn1" title="footnote 1">1</a></sup> extension and XPath<sup><a href="#fn2" title="footnote 2">2</a></sup> expressions. </p>
<p>The (<del datetime="2009-10-26T14:15:44Z">currently undocumented</del><ins datetime="2009-10-26T14:15:44Z">now documented</ins><sup><a href="#fn3" title="footnote 3">3</a></sup>) <code>DOMXPath::registerPHPFunctions</code> method is available as of PHP 5.3.0 (it was added to the code base back in December 2006) and allows the use of PHP functions (and static methods) within XPath queries to complement the normal set of XPath functions<sup><a href="#fn2" title="footnote 2">2</a></sup>.<span id="more-336"></span></p>
<h3>Description</h3>
<p><code>void <strong>DOMXPath::registerPHPFunctions</strong>  ([ string|array <var>$restrict</var>] )</code></p>
<p>Enables the use of PHP functions as XPath functions.</p>
<h3>Parameters</h3>
<dl>
<dt>restrict</dt>
<dd>
Use this parameter to only allow certain functions to be called from XPath; it can be either a string (a function name) or an array of function names.
</dd>
</dl>
<h3>Return Values</h3>
<p>No value is returned.</p>
<h3>Examples</h3>
<p><strong>Note:</strong> The following examples load a sample XML document called <kbd>book.xml</kbd> with the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" ><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP Basics<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jim Smith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jane Smith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP Secrets<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jenny Smythe<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>XML basics<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Joe Black<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3 id="ex1">Example #1: Call a PHP function in XPath with <code>php:functionString</code></h3>
<p>This example demonstrates the basic use of <code>DOMXPath::registerPHPFunctions</code> by replicating the <code>substring</code> XPath function. The first thing that needs to be done is to register the <samp>php</samp> namespace with the associated URI <samp>http://php.net/xpath</samp>. Don&#8217;t question it, it just needs to be done!</p>
<p>Next, we call <code>DOMXPath::registerPHPFunctions</code> on our object. If no arguments are used, as in this example, then the range of functions allowed to be called is not restricted—you can call any<sup><a href="#fn4" title="footnote 4">4</a></sup> function from XPath. I would always advise restricting the functions which can be called, see <a href="#ex2" title="Example 2">example 2</a>.</p>
<p>Within our XPath query, we use <code>php:functionString</code> which allows us to name a function and provide some parameters (or indeed, no parameters) to be passed to that function. There are two flavours which can be used here: <code>php:functionString</code> which passes an XML node/attribute as a string and <code>php:function</code> (see <a href="#ex3" title="Example 3">example 3</a>) which passes an array of XML node/attribute objects (in <code>DOMElement</code> / <code>DOMAttr</code> / etc. form) to the function .  In this example the PHP function <a href="http://php.net/substr"><code>substr</code></a> is called, passing along the book&#8217;s title (in string form), an offset of <samp>0</samp> and length of <samp>3</samp>. This returns the first 3 characters of the book&#8217;s title which is then compared to the string <kbd>PHP</kbd> in order to filter our list of books down to those having titles starting with <kbd>PHP</kbd>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Call substr function on the book title</span>
<span style="color: #000088;">$nodes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//book[php:functionString(&quot;substr&quot;, title, 0, 3) = &quot;PHP&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Found <span style="color: #006699; font-weight: bold;">{$nodes-&gt;length}</span> books starting with 'PHP':<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$title</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$author</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;author&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$title</span> by <span style="color: #006699; font-weight: bold;">$author</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The example will output something like:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >Found 2 books starting with 'PHP':
PHP Basics by Jim Smith
PHP Secrets by Jenny Smythe</pre></div></div>

<h3 id="ex2">Example #2: Restricting the functions available to XPath</h3>
<p>To restrict the functions made available to XPath, provide either a string containing the name of the single function that you wish to allow or an array of strings containing function names as the <var>restrict</var> parameter (note: static methods can also be used, e.g. &#8220;<samp>Classname::method</samp>&#8220;).  If functions were added with <var>restrict</var> and a function is called in XPath which is not one of them, an <var>E_WARNING</var> will be raised stating <samp>Not allowed to call handler &#8216;<var>function</var>()&#8217;</samp> (where <var>function</var> is the name of the function that cannot be called).</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;strtoupper&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get first book's title in uppercase</span>
<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php:functionString(&quot;strtoupper&quot;, //book[1]/title)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Try a function not in our restrictions list</span>
<span style="color: #000088;">$fail</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php:functionString(&quot;strtolower&quot;, //book[1]/title)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The example will output something like:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >PHP BASICS&lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;:  DOMXPath::evaluate() [&lt;a href='domxpath.evaluate'&gt;domxpath.evaluate&lt;/a&gt;]: Not allowed to call handler 'strtolower()'. in &lt;b&gt;example2.php&lt;/b&gt; on line &lt;b&gt;18&lt;/b&gt;&lt;br /&gt;</pre></div></div>

<h3 id="ex3">Example #3: Passing DOM objects using <code>php:function</code></h3>
<p>Up to now, the examples have both used <code>php:functionString</code>. As mentioned above, instead of passing a string value to the PHP function it is possible to pass along an array of DOM* objects to manipulate them as you please by using <code>php:function</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span>
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;example3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> example3<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Return true if more than one author</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Filter books with multiple authors</span>
<span style="color: #000088;">$books</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//book[php:function(&quot;example3&quot;, author)]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Books with multiple authors:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$book</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The example will output something like:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >Books with multiple authors:
PHP Basics</pre></div></div>

<h3>Summary</h3>
<p>Just to quickly summarise everything, here is a quick run-down. In PHP, make sure to register the <kbd>php</kbd> namespace (with the URI <kbd>http://php.net/xpath</kbd>) and then register your PHP functions (whether core, extensions or user-defined) or static methods with <code>DOMXPath::registerPHPFunctions</code>. In XPath, use <code>php:functionString</code> or <code>php:function</code> to call the PHP function.</p>
<p>If you have made use of this feature, or want to know more, then do feel free to comment. Thanks for reading.</p>
<h4>Footnotes</h4>
<ol>
<li id="fn1"><a href="http://php.net/dom" title="PHP: DOM">http://php.net/dom</a></li>
<li id="fn2"><a href="http://schlitt.info/opensource/blog/0704_xpath.html" title="XPath overview">http://schlitt.info/opensource/blog/0704_xpath.html</a>
<li id="fn3">The documentation page, <del datetime="2009-10-26T14:15:44Z">when it gets written, will be</del><ins datetime="2009-10-26T14:15:44Z">is</ins> available at <a href="http://php.net/domxpath.registerphpfunctions" title="PHP: DOMXPath::registerPHPFunctions">http://php.net/domxpath.registerphpfunctions</a></li>
<li id="fn4">In truth, some functions are not suitable such as those that return non-scalar values (and cannot be cast to one) which XPath will not understand.</li>
</ol>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=WojNZ1aU-30:kBhcs3F55rk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=WojNZ1aU-30:kBhcs3F55rk:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/WojNZ1aU-30" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/10/23/php-funcs-xpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/10/23/php-funcs-xpath/</feedburner:origLink></item>
		<item>
		<title>Comments on “Creating a Crypter Class with PHP”</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/xd9KlHW5JX4/</link>
		<comments>http://cowburn.info/2009/09/28/nettuts-crypter/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 20:22:02 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[crypter]]></category>
		<category><![CDATA[nettuts]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=326</guid>
		<description><![CDATA[The following was supposed to be a comment to the Nettuts+ article published recently entitled Creating a Crypter Class with PHP. The powers that be over there seem not to want to moderate the comment so I&#8217;ll publish it here and hopefully the trackback will connect things together. The comment is after the fold and [...]]]></description>
			<content:encoded><![CDATA[<p>The following was supposed to be a comment to the <a href="http://net.tutsplus.com" title="Web Development &#038; Design Tutorials - Nettuts+">Nettuts+</a> article published recently entitled <a href="http://net.tutsplus.com/tutorials/php/creating-a-crypter-class/" title="Read 'Creating a Crypter Class with PHP'">Creating a Crypter Class with PHP</a>.  The powers that be over there seem not to want to moderate the comment so I&#8217;ll publish it here and hopefully the trackback will connect things together. The comment is after the fold and it would make sense to perhaps at least scan over <a href="http://net.tutsplus.com/tutorials/php/creating-a-crypter-class/" title="Read 'Creating a Crypter Class with PHP'">Christian&#8217;s article</a> before reading my comments.<span id="more-326"></span></p>
<h3>Original Comment (#112314)</h3>
<blockquote cite="http://net.tutsplus.com/tutorials/php/creating-a-crypter-class/#comment-112314"><p>
Hi Christian, thank you for the article. I would like to share a few comments if I may.</p>
<p>You seem to create the interface (ICrypter) almost without considering why an interface might be necessary. The article skirts around the reasons why an interface might be handy, instead preferring to mention that any class which implements a given interface must adhere to it (other than, “there will be an error!”). It is far outside the realm of a simple blog comment to delve into this subject so I can only suggest that readers take it upon themselves to build their own understanding of interfaces in general and in PHP. There is a lot of great information out there (for a recent insight into the subject, see <a href="http://www.brandonsavage.net/why-interfaces-rock/" title="Why Interfaces Rock | BrandonSavage.net">http://www.brandonsavage.net/why-interfaces-rock/</a> ).</p>
<p>The article doesn’t state anywhere (though it will be mightily obvious when a reader tries to use the code) that MCrypt is required. Readers, see <a href="http://php.net/mcrypt.requirements" title="PHP: MCrypt Requirements">http://php.net/mcrypt.requirements</a> and <a href="http://mcrypt.sourceforge.net/" title="MCrypt">http://mcrypt.sourceforge.net/</a></p>
<p>With regards to the list of “supported algorithms from php.net” it would have been nice to have a source cited for reference. See <a href="http://php.net/mcrypt.ciphers" title="PHP: MCrypt Ciphers">http://php.net/mcrypt.ciphers</a></p>
<p>The remarks about Base64 encoding/decoding values seems a little odd. Why would you automatically want to do this? Your argument is that the encrypted value might not be URL-safe but that is only an issue if the encrypted value is being placed in an URL. If you do need to use the encrypted value in an URL then it would make more sent to encode the value only when it needs to be. Base64 is not the only available option for making such a value URL-safe (the functions urlencode and http_build_query to name a few).</p>
<p>It also puzzles me that you use the trim function on the Base64-encoded value since there will not be any whitespace present to be trimmed! The same goes for trimming the decrypted value since there may be important whitespace which really should not be trimmed. </p>
<p>The article itself does not elude to why this helper class might be useful. Your comments state there will be a follow-up article with a more practical demonstration but sure it would have been nice to include a very brief situation and code example where this type of encryption/decryption is of particular benefit.</p>
<p>I guess that is enough writing for a blog comment! Congratulations on publishing your first article on Nettuts+ and I look forward to your next ones.</p></blockquote>
<p>The above is my comment, copied verbatim (with the links manually added since WordPress didn&#8217;t want to do that). Someone probably just missed by comment in the queue, so whether it appears on the Nettuts+ site or not remains to be seen. Either way the full comment is available here for reference.</p>
<p class="small" datetime="2009-09-28T21:45:12Z">Edit: between publishing this (at 8:22pm) and now (10:45pm) the comment got approved and is up on the Nettuts+ website. Guess I was just slightly too quick out of the blocks in republishing it here.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=xd9KlHW5JX4:uZ4w9Rp3W9E:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=xd9KlHW5JX4:uZ4w9Rp3W9E:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/xd9KlHW5JX4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/09/28/nettuts-crypter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/09/28/nettuts-crypter/</feedburner:origLink></item>
		<item>
		<title>The InfiniteIterator in PHP</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/IXtnrxwCnu8/</link>
		<comments>http://cowburn.info/2009/09/25/infiniteiterator/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 12:49:28 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[infiniteiterator]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=316</guid>
		<description><![CDATA[In an article back in July (Anonymous Functions and Closures (as of PHP 5.3)), I gave an example of looping over a series of values repeatedly. Whilst that example does the job (and introduces the concept of closures) it&#8217;s hardly the most convenient method of repeatedly iterating over a series of values. Introducing the InfiniteIterator [...]]]></description>
			<content:encoded><![CDATA[<p>In an article back in July (<a href="http://cowburn.info/2009/07/08/anonymous-closures/" title="Read Anonymous Functions and Closures (as of PHP 5.3)">Anonymous Functions and Closures (as of PHP 5.3)</a>), I gave <a href="http://cowburn.info/2009/07/08/anonymous-closures/#fig5" title="See example: Cycling with Closures">an example</a> of looping over a series of values repeatedly.  Whilst that example does the job (and introduces the concept of closures) it&#8217;s hardly the most convenient method of repeatedly iterating over a series of values.  Introducing the <a href="http://php.net/InfiniteIterator" title="PHP Manual: InfiniteIterator"><code>InfiniteIterator</code></a> which is part of the <a href="http://php.net/spl" title="Standard PHP Library">Standard PHP Library</a> (SPL).<span id="more-316"></span></p>
<p>On the plus side, for those who have yet to take the plunge into using PHP 5.3, this iterator has been part of the core of PHP as of 5.1.0 so there is a much greater chance of actually being able to use it right now.  Bear in mind that the SPL is supposed to work together with its component parts so if <a href="http://php.net/iterator" title="PHP: Object Iteration">iterators</a> are a foreign concept, some of this post might be a little unclear but it is not the purpose of this post to outline iterators in PHP.</p>
<p>As eluded to above, the <code>InfiniteIterator</code> comes in useful when you have an existing <code>Iterator</code> (anything that implements that interface; a directory listing (<code>DirectoryIterator</code>), a file (<code>SplFileObject</code>), an array (with <code>ArrayIterator</code>), and so on) and wish to iterate over its contents again and again. </p>
<h3>An <code>InfiniteIterator</code> Example</h3>
<p>Here is a basic example that I wrote for the <a href="http://php.net/InfiniteIterator.construct" title="PHP Manual: InfiniteIterator::__construct">documentation</a>. Note that a <a href="http://php.net/LimitIterator" title="The LimitIterator class"><code>LimitIterator</code></a> is used to restrict the values which are iterated over (otherwise the loop would go on forever!).</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$arrayit</span>  <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayIterator<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'dog'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$infinite</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InfiniteIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrayit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$limit</span>    <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LimitIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$infinite</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$value</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This example outputs something along the lines of:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" >cat
dog
cat
dog
cat
dog
cat</pre></div></div>

<p>Can you think of any more useful instances where the <code>InfiniteIterator</code> might come in useful?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=IXtnrxwCnu8:KJMFyRqfXJQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=IXtnrxwCnu8:KJMFyRqfXJQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/IXtnrxwCnu8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/09/25/infiniteiterator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/09/25/infiniteiterator/</feedburner:origLink></item>
		<item>
		<title>What Firefly/Serenity Can Teach Us About PHP (external)</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/3I57NFHwMQA/</link>
		<comments>http://cowburn.info/2009/08/11/firefly-php/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 14:36:12 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[external]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=303</guid>
		<description><![CDATA[This update is just really linking through to What Firefly/Serenity Can Teach Us About PHP on PHP Developer where the author makes some comparison between the awesome sci-fi series Firefly and PHP. Worth a few minutes at least: go read. Here&#8217;s the summary Just because it’s not the latest and greatest version, it doesn’t mean [...]]]></description>
			<content:encoded><![CDATA[<p>This update is just really linking through to <a href="http://blog.phpdeveloper.org/?p=158">What Firefly/Serenity Can Teach Us About PHP</a> on <a href="http://blog.phpdeveloper.org/">PHP Developer</a> where the author makes some comparison between the awesome sci-fi series Firefly and PHP. Worth a few minutes at least: <a href="http://blog.phpdeveloper.org/?p=158">go read</a>.<span id="more-303"></span></p>
<p>Here&#8217;s the summary</p>
<ul>
<li>Just because it’s not the latest and greatest version, it doesn’t mean it’s not good</li>
<li>Just because you don’t understand something, it doesn’t mean you shouldn’t embrace it</li>
<li>There’s nothing stronger than a bond between those who care</li>
<li>It takes all kinds to make the &#8216;verse go &#8217;round</li>
<li>Use only what you need</li>
<li>A good team is nothing without good leaders</li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=3I57NFHwMQA:datoZJtH480:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=3I57NFHwMQA:datoZJtH480:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/3I57NFHwMQA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/08/11/firefly-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/08/11/firefly-php/</feedburner:origLink></item>
		<item>
		<title>Feedburner Stats with YQL</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/38t5ZFuR7zA/</link>
		<comments>http://cowburn.info/2009/07/19/feedburner-stats-with-yql/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 23:15:17 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[YQL]]></category>
		<category><![CDATA[data table]]></category>
		<category><![CDATA[feedburner]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=282</guid>
		<description><![CDATA[A short while ago (read: earlier today) I got the urge to add the (tiny!) subscriber count onto my blog here: mostly so that I can see the fluctuations without having to log into the Feedburner site and click around, but also to show the lucky few of you who do visit that you&#8217;re in [...]]]></description>
			<content:encoded><![CDATA[<p>A short while ago (read: earlier today) I got the urge to add the (<em>tiny!</em>) subscriber count onto my blog here: mostly so that I can see the fluctuations without having to log into the Feedburner site and click around, but also to show the lucky few of you who do visit that you&#8217;re in esteemed company! Feedburner has a really nice little <a href="http://code.google.com/apis/feedburner/" title="Feedburner API on Google Code">API</a> which could be used directly except that I couldn&#8217;t find a <abbr title="JavaScript Object Notation">JSON</abbr> output format from it (to use via JavaScript) and, well, I wanted to keep playing with <a href="http://developer.yahoo.com/yql/" title="Yahoo! Query Language">YQL</a>. So here&#8217;s what I did.<span id="more-282"></span></p>
<p>Building on the idea of <a href="http://datatables.org/" title="YQL - Community Open Data Tables">open data tables</a>, I made a very quick table (<a href="http://github.com/salathe/yql-tables/blob/master/feedburner/feedburner.feed.xml">see it on GitHub</a>) which plugs into Feedburner&#8217;s API and allows us, with a few different options, to grab data about a specific Feedburner feed by using queries like <code>select * from feedburner.feed where uri="cowburn"</code><sup><a href="#footnote1">1</a></sup>.</p>
<h3>Querying against the table</h3>
<p>Since the table is just my own, and not part of the main collection of contributed tables, we have to tell YQL about its location before we can do any queries on it. To do that, simply use <code>use</code> pointing to the url of the <abbr title="eXtensible Markup Language">XML</abbr> file: <code>use "http://github.com/salathe/yql-tables/raw/master/feedburner/feedburner.feed.xml";</code></p>
<p>Once YQL knows about the table, we can query against it (by default the table name is constructed from the file name, <var>feedburner.feed.xml</var> means we use <samp>feedburner.feed</samp> as the table name).  The table is set up to allow three different methods of grabbing the feed data.</p>
<h4>1. Up-to-date Statistics</h4>
<p>If just the feed name is provided then only the most up-to-date information about that feed will be returned. Feedburner does not let you get stats about today, so the most recent data will always be from yesterday.  So <code>select * from feedburner.feed where uri="cowburn"</code><sup><a href="#footnote1">1</a></sup> gives one result containing yesterdays statistics; subscriber count (<var>circulation</var>), the <var>date</var>, the number of <var>hits</var> the feed got and the <a href="http://www.google.com/support/feedburner/bin/answer.py?hl=en&#038;answer=78954" title="What is reach?"><var>reach</var></a> of the feed. </p>
<h4>2. Get last <var>n</var> days of stats</h4>
<p>If you&#8217;re after more than just the latest figures, simply add a <var>days</var> clause to the <var>where</var> portion of the query: <code>… where uri="cowburn" and days="7"</code><sup><a href="#footnote2">2</a></sup></p>
<h4>3. Get a specific set of dates</h4>
<p>If the <em>last</em> <var>n</var> days is too recent for you, there&#8217;s also the option of passing along a range or series of dates with, conveniently enough, a <var>dates</var> clause. This value follows the format for <a href="http://code.google.com/apis/feedburner/awareness_api.html#dates">formatting date ranges</a> of the Feedburner API. So to select feed data between July first and seventh from 2009 you would simply add <code>… where uri="cowburn" and dates="2009-07-01,2009-07-07"</code><sup><a href="#footnote3">3</a></sup></p>
<p>Note that you cannot combine the <var>days</var> and <var>dates</var> in the same query as that would be silly.</p>
<h3>Adding Subscriber Count to the Blog</h3>
<p>To add the subscriber count to the page (next to the &#8220;subscribe&#8221; link in the header), just takes a few lines of JavaScript (I used jQuery) to pull a JSON-formatted response back from YQL. The console even automatically creates an URL for you to copy and paste in the section entitled &#8220;<q cite="http://developer.yahoo.com/yql/console/">The REST query</q>&#8220;. </p>
<p>The actual jQuery/JS code is not particularly noteworthy, all it does is prepare the URL to the YQL API, asks that URL for some JSON and then parses the response into a HTML <var>span</var> tag which is injected within the &#8220;subscribe&#8221; link of the navigation.  This blog post is more about the data table, than grabbing JSON and injecting elements into the DOM (which is super-easy with jQuery anyway). Would you like that process described a bit more thoroughly? My <a href="http://cowburn.info/2009/01/20/feed-widget-with-yql-jquery/">feed widget with YQL and jQuery</a> post shows you some actual code.</p>
<p>Well that&#8217;s all there is to say really. If you want to use the Feedburner data table then by all means feel free—it&#8217;s up there <a href="http://github.com/salathe/yql-tables/blob/master/feedburner/feedburner.feed.xml" title="feedburner.feed table on GitHub">on GitHub</a> so you can link to the file or fork and make your own changes.</p>
<h5 class="small">Footnotes</h5>
<ol class="small">
<li id="footnote1"><a href="http://developer.yahoo.com/yql/console/?q=use%20%22http%3A%2F%2Fgithub.com%2Fsalathe%2Fyql-tables%2Fraw%2Fmaster%2Ffeedburner%2Ffeedburner.feed.xml%22%3B%20select%20*%20from%20feedburner.feed%20where%20uri%3D%22cowburn%22" title="Execute this query in the YQL console">Try <code>select * from feedburner.feed where uri="cowburn"</code> in the YQL console</a></li>
<li id="footnote2"><a href="http://developer.yahoo.com/yql/console/?q=use%20%22http%3A%2F%2Fgithub.com%2Fsalathe%2Fyql-tables%2Fraw%2Fmaster%2Ffeedburner%2Ffeedburner.feed.xml%22%3B%20select%20*%20from%20feedburner.feed%20where%20uri%3D%22cowburn%22%20and%20days%3D%227%22" title="Execute this query in the YQL console">Try <code>select * from feedburner.feed where uri="cowburn" and days="7"</code> in the YQL console</a></li>
<li id="footnote3"><a href="http://developer.yahoo.com/yql/console/?q=use%20%22http%3A%2F%2Fgithub.com%2Fsalathe%2Fyql-tables%2Fraw%2Fmaster%2Ffeedburner%2Ffeedburner.feed.xml%22%3B%20select%20*%20from%20feedburner.feed%20where%20uri%3D%22cowburn%22%20and%20dates%3D%222009-07-01%2C2009-07-07%22" title="Execute this query in the YQL console">Try <code>select * from feedburner.feed where uri="cowburn" and dates="2009-07-01,2009-07-07"</code> in the YQL console</a>
</ol>
<p>P.S. I <em>promise</em> (with crossed fingers just in case) that the next blog post will not be about YQL! <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=38t5ZFuR7zA:nEP-Iis0-bQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=38t5ZFuR7zA:nEP-Iis0-bQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/38t5ZFuR7zA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/07/19/feedburner-stats-with-yql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/07/19/feedburner-stats-with-yql/</feedburner:origLink></item>
		<item>
		<title>YQL keeps improving</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/aPU_Wnnj89Y/</link>
		<comments>http://cowburn.info/2009/07/09/yql-keeps-improving/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 10:45:01 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[YQL]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Yahoo]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=253</guid>
		<description><![CDATA[I missed this yesterday but the YQL team have pushed out a whole heap of changes to their awesome service. A full list of changes can be found in the changelog but my favourites have to be (in no particular order): INSERT/UPDATE/DELETE, post/put/delete and a bug fix allowing remote JSON to have a top-level array. [...]]]></description>
			<content:encoded><![CDATA[<p>I missed this yesterday but the <a href="http://developer.yahoo.com/yql/" title="Yahoo! Query Language">YQL</a> team have pushed out a whole heap of changes to their awesome service.  A full list of changes can be found in <a href="http://www.yqlblog.net/blog/2009/07/08/changelog-for-build-2174/" title="Changelog for build 2174">the changelog</a> but my favourites have to be (in no particular order): INSERT/UPDATE/DELETE, post/put/delete and a bug fix allowing remote JSON to have a top-level array.<span id="more-253"></span></p>
<h3><a href="http://developer.yahoo.com/yql/guide/yql-iud-statements.html" title="I/U/D Statements">INSERT/UPDATE/DELETE</a></h3>
<p>Now we can have a full CRUD stack to play with enabling use to write to the web in addition to the reading we&#8217;ve had thus far! If you&#8217;re failing to see the juiciness, think that you can now do <code>INSERT INTO web.service (a, b, c) VALUES ("a", "b", "c")</code> where <var>web.service</var> can be pretty much any open API (<a href="http://developer.yahoo.com/yql/guide/yql-iud-examples.html#yql-iud-example-bitly" title="INSERT INTO with bit.ly and Twitter">bit.ly and twitter examples</a> are in the documentation) or, to put it plainly, pretty much any web site which accepts user input. </p>
<h3>post/put/delete methods on the <a href="http://developer.yahoo.com/yql/guide/yql-javascript-objects.html#yql-execute-yrestobject">y.rest()</a> method</h3>
<p>A <em>huge</em> thank you for this since, tied into the above on I/U/D, this enables us to push back data to the web. I <a href="http://developer.yahoo.net/forum/index.php?showtopic=1326">asked for this</a> back in May since I needed it to log into a website via a POST request, saving cookies for the main request after login.</p>
<h3>Json table now accepts top-level arrays.</h3>
<p>A small bug fix but particularly useful for me. See <a href="http://developer.yahoo.net/forum/index.php?showtopic=1706" title="YQL Forum post">my YQL forum post on this bug</a> which arose due to my trying to query a <a href="http://delicious.com/help/feeds" title="Delicious feeds documentation">delicious.com JSON feed</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=aPU_Wnnj89Y:ntd2xJKtNmc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=aPU_Wnnj89Y:ntd2xJKtNmc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/aPU_Wnnj89Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/07/09/yql-keeps-improving/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/07/09/yql-keeps-improving/</feedburner:origLink></item>
	</channel>
</rss>
