<?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, 04 Mar 2010 21:51:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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.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>

		<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 [...]]]></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 [...]]]></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 it’s not [...]]]></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 [...]]]></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>
		<item>
		<title>Anonymous Functions and Closures (as of PHP 5.3)</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/1IfK4klTVRQ/</link>
		<comments>http://cowburn.info/2009/07/08/anonymous-closures/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 14:41:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[anonymous functions]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=234</guid>
		<description><![CDATA[There has been much discussion, especially of late considering the release of PHP 5.3, on the subject of anonymous functions and closures (both terms appear to be used interchangeably in the documentation).  They are not the same, though many folks are introduced to them at the same time.
Anonymous Functions
Put plainly, an anonymous function is [...]]]></description>
			<content:encoded><![CDATA[<p>There has been much discussion, especially of late considering the release of PHP 5.3, on the subject of anonymous functions and closures (both terms appear to be used interchangeably in the <a href="http://php.net/closures" title="Closures in the PHP Manual">documentation</a>).  They are not the same, though many folks are introduced to them at the same time.<span id="more-234"></span></p>
<h3>Anonymous Functions</h3>
<p>Put plainly, an anonymous function is a function without a name. That&#8217;s all, there&#8217;s nothing magic or particularly technical about it.  There are a few ways to create an anonymous function in PHP; up until PHP 5.3 came around the usual way was to use <a href="http://php.net/create_function" title="create_function documentation on php.net"><code>create_function</code></a> (see <a href="#fig1" title="View figure 1: Anonymous function with create_function">fig. 1</a>) which takes a couple of arguments as strings and spits out a function, in PHP 5.3 we&#8217;re allowed to create anonymous functions looking much more like normal function definitions (see <a href="#fig2" title="View figure 2: Anonymous function in PHP 5.3">fig. 2</a>).</p>
<h4 id="fig1">Fig. 1: Anonymous function with create_function</h4>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return $x*$x;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h4 id="fig2">Fig 2: Anonymous function as of PHP 5.3</h4>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>I hope you&#8217;ll agree that the latter is a much nicer way (visually) of defining an anonymous function, quite apart from the more logical and performance-related (see <a href="http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures#comments" title="Comments on 'On PHP 5.3, Lambda Functions, and Closures'">comment #4</a>) aspects of using it.</p>
<h3>Closures</h3>
<p>A closure is a (anonymous) function that is aware of its context. For all intents and purposes, think of them being anonymous functions which know about some variables which weren&#8217;t defined within them. In geeky language, they are functions which close over (hence &#8220;closure&#8221;) free variables, the latter being variables which are not local nor arguments to the function.  It is easier to see with an example, I think.</p>
<h4 id="fig3">Without Closure</h4>
<p>Lets say that we need a function to add three to a number (overly basic, I know). That&#8217;s easy.</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;">$add_3</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_3</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 13</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now if we wanted another function to behave similarly but instead it should add 4 to the supplied number, we could just as easily create a new anonymous function to do the job.</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;">$add_4</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_4</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 14</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If, however, you wanted to do that for a series of different additions then things could get very repetitive, very quickly!  If only we could dynamically create an anonymous function to do what we wanted: enter the closure.</p>
<h4 id="fig4">With Closure</h4>
<p>We can define an anonymous function (it needn&#8217;t be anonymous but lets keep on theme) which returns anonymous function based on arguments supplied to it. Remember our definition of a closure above, specifically making use of &#8216;free&#8217; variables, that is what our <code>$value_to_add</code> is in the example below.</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;">$add</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value_to_add</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> use <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value_to_add</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$value_to_add</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$add_3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$add_4</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_3</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 13</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_4</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 14</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h3>A more practical example</h3>
<p>As fun as silly adding examples are, there will be undoubtedly someone who has been reading everything above (good job getting this far!) and cannot see a practical use for all of this. Since <a href="http://www.letour.fr/" title="Site officiel du tour de France">le Tour de France</a> is on, lets have a play on words and have an example on cycling (over a series of values).</p>
<h4 id="fig5">Cycling with Closures</h4>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Cycle factory: takes a series of arguments</span>
<span style="color: #666666; font-style: italic;">// for the closure to cycle over.</span>
<span style="color: #000000; font-weight: bold;">function</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Create closure using the supplied list of arguments</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> use <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Get current item</span>
        <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Rewind if at the end of the list</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// Return the current item</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$current</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create our list of CSS classes to cycle over</span>
<span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'odd'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'even'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// List of adjectives, why not</span>
<span style="color: #000088;">$taste</span> <span style="color: #339933;">=</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vile'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yummy'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delicious'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Our list of items to be formatted into a pretty HTML list</span>
<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cherry'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'elderberry'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'figs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;ul&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;li class=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> are <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$taste</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/li&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/ul&gt;</pre></div></div>

<p>That example would provide the following output:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" ><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>apples are vile<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>bananas are yummy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>cherries are delicious<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>dates are vile<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>elderberries are yummy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>figs are delicious<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></div></div>

<p>Well, it&#8217;s a little bit more practical anyway!  Have you been playing with closures and/or anonymous functions too?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=1IfK4klTVRQ:hDKUjAnkhhw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=1IfK4klTVRQ:hDKUjAnkhhw:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/1IfK4klTVRQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/07/08/anonymous-closures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/07/08/anonymous-closures/</feedburner:origLink></item>
		<item>
		<title>PHP 5.3.0 Released</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/XKvozB4mQLA/</link>
		<comments>http://cowburn.info/2009/06/30/php-530/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 13:12:45 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php5.3.0]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=224</guid>
		<description><![CDATA[Today the latest stable version of PHP was announced, PHP 5.3.0.  
Go download and play with the new features available such as namespaces, LSB, anonymous functions and closures along with many more bug fixes, additions and improvements.
My favourite addition at the moment is the improvements made to the DateTime extension with new classes such [...]]]></description>
			<content:encoded><![CDATA[<p>Today the latest stable version of PHP was announced, <a href="http://php.net/releases/5_3_0.php" rel="external">PHP 5.3.0</a>.  </p>
<p>Go download and play with the new features available such as <a href="http://php.net/namespaces">namespaces</a>, <a href="http://php.net/lsb" title="late static binding">LSB</a>, <a href="http://php.net/closures">anonymous functions and closures</a> along with many more bug fixes, additions and improvements.<span id="more-224"></span></p>
<p>My favourite addition at the moment is the improvements made to the <a href="http://php.net/datetime">DateTime</a> extension with new classes such as <a href="http://php.net/dateperiod">DatePeriod</a> (for iterating over dates/times) and <a href="http://php.net/dateinterval">DateInterval</a> and additions to the DateTime class such as <a href="http://php.net/datetime.createFromFormat">DateTime::createFromFormat</a> and <a href="http://php.net/datetime.add">DateTime::add</a> (and sub).</p>
<p>The anonymous functions will also be very useful where one might previously use <a href="http://php.net/create_function">create_function</a> (or a normal non-anonymous function) in particular for callbacks to other functions/methods.</p>

<div class="wp_syntax"><div class="code"><pre class="php" ><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[A-Z]/'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">str_rot13</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Uello Jorld!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So, download it and have an explore. <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=XKvozB4mQLA:cTn1yAxd8Rc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=XKvozB4mQLA:cTn1yAxd8Rc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/XKvozB4mQLA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/06/30/php-530/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/06/30/php-530/</feedburner:origLink></item>
		<item>
		<title>XPath for CSS classes</title>
		<link>http://feedproxy.google.com/~r/cowburn/~3/JwouQqXlud4/</link>
		<comments>http://cowburn.info/2009/06/15/xpath-css-class/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 19:18:57 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[XPath]]></category>
		<category><![CDATA[contains]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[normalize-space]]></category>
		<category><![CDATA[YQL]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=203</guid>
		<description><![CDATA[I was playing around with YQL today and was scraping a HTML document (no details, that&#8217;s not the point of this blog entry).  Part of the processes meant that I needed to check for a specific CSS class attached to some elements. 
Easy enough, XPath contains (pardon the pun, see later) some nifty functions [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing around with <a href="http://developer.yahoo.com/yql/" title="Yahoo! Query Language">YQL</a> today and was scraping a HTML document (no details, that&#8217;s not the point of this blog entry).  Part of the processes meant that I needed to check for a specific CSS class attached to some elements. </p>
<p>Easy enough, XPath contains (pardon the pun, see later) some nifty functions which can act on nodes and their values to be used in conditional checks like I needed. I&#8217;ve been using the technique outlined in this post for a very long while (usually with XPath in PHP) and originally stumbled upon the basic idea from <a href="http://plasmasturm.org/log/444/">How to map CSS selectors to XPath queries</a> (Thanks ever so much!) Today&#8217;s use of it prompted this blog post. <span id="more-203"></span></p>
<p>For example, to grab any <code>li</code> elements with a class of <var>active</var> (and only that one class) out of the document it would be easy enough to use:</p>

<div class="wp_syntax"><div class="code"><pre class="xpath" >//li[@class=&quot;active&quot;]</pre></div></div>

<p>Now, as I said that will only grab the list items which have that specific class like <code>&lt;li class="active"&gt;</code>.  What if there is more than one class in there, ala <code>&lt;li class="hot active"&gt;</code>? We could use the XPath function <code>contains</code> which looks to see if a string contains another string but that would cause problems (e.g. searching for &#8220;a&#8221; in conjunction with classes like &#8220;active&#8221;, &#8220;nav&#8221;, etc. would result in false positives).</p>
<p>To match any list item with a classes which contains the letter &#8220;a&#8221; can be sought with:</p>

<div class="wp_syntax"><div class="code"><pre class="xpath" >//li[contains(@class, &quot;a&quot;)]</pre></div></div>

<p>Nearly there, now to work out how to not match false positives.  To do this, we can look for &#8220;<code>[space]a[space]</code>&#8221; with a slightly modified class string (classes at the  start/end of the string wouldn&#8217;t normally have spaces before/after respectively). Whilst we&#8217;re at it, we can <em>normalize</em> the spaces in the class string which converts any number of spaces, tabs, etc. characters into just a single space.</p>
<p>So, to properly get at a class name of &#8220;<var>active</var>&#8221; for all list items, regardless of its position in the class string, the following XPath query can be used.</p>

<div class="wp_syntax"><div class="code"><pre class="xpath" >//li[contains(concat(&quot; &quot;, normalize-space(@class), &quot; &quot;), &quot; active &quot;)]</pre></div></div>

<p><span class="aside">Note: there are single spaces in the first and last arguments for <code>concat</code>, and on either side of the word <code>active</code></span>.</p>
<p>Just as an observation, the comparable CSS selector to do this would be <code>li[class~="active"]</code> (or the more usual <code>li.active</code>) which would be awesome to use if XPath decided to implement a similar syntax.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cowburn?a=JwouQqXlud4:vh1NkIwm1Z8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/cowburn?i=JwouQqXlud4:vh1NkIwm1Z8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cowburn/~4/JwouQqXlud4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/06/15/xpath-css-class/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://cowburn.info/2009/06/15/xpath-css-class/</feedburner:origLink></item>
	</channel>
</rss>
