<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>phoenixheart - portfolio &#38; more &#187; phpquery</title> <atom:link href="http://www.phoenixheart.net/tag/phpquery/feed/" rel="self" type="application/rss+xml" /><link>http://www.phoenixheart.net</link> <description>phoenixheart - portfolio &#38; more</description> <lastBuildDate>Wed, 25 Aug 2010 03:02:03 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <script type="text/javascript">/*<![CDATA[*/if(typeof Meebo=="undefined"){Meebo=function(){(Meebo._=Meebo._||[]).push(arguments)};(function(q){var args=arguments;if(!document.body){return setTimeout(function(){args.callee.apply(this,args)},100);}var d=document,b=d.body,m=b.insertBefore(d.createElement('div'),b.firstChild);s=d.createElement('script');m.id='meebo';m.style.display='none';m.innerHTML='<iframe id="meebo-iframe"></iframe>';s.src='http'+(q.https?'s':'')+'://'+(q.stage?'stage-':'')+'cim.meebo.com/cim/cim.php?network='+q.network;b.insertBefore(s,b.firstChild);})({network:'phoenixheartnet_bo16we'});}/*]]>*/</script> <item><title>phpQuery</title><link>http://www.phoenixheart.net/2009/03/phpquery/</link> <comments>http://www.phoenixheart.net/2009/03/phpquery/#comments</comments> <pubDate>Mon, 09 Mar 2009 18:09:36 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[phpquery]]></category><guid
isPermaLink="false">http://www.phoenixheart.net/?p=252</guid> <description><![CDATA[It&#8217;s been quite a while I don&#8217;t have any update on this blog (sorry my *ten* RSS subscribers&#8230;). If there is any excuse, then it should be the fact that I&#8217;ve just left Gameloft to join a start up, and you know it, the work load is just a bit heavier than what I expected. [...]]]></description> <content:encoded><![CDATA[<p>It&#8217;s been quite a while I don&#8217;t have any update on this blog (sorry my *ten* RSS subscribers&#8230;). If there is any excuse, then it should be the fact that I&#8217;ve just left Gameloft to join a start up, and you know it, the work load is just a bit heavier than what I expected. To make my life somewhat more difficult, one day I realized that the current theme of this blog was just too messy and not so flexible, so I decided to redesign it somehow. Well, the new theme is under development now, and all I can promise is it will be much easier for the eye.</p><p>Ok cut the chit-chat, today I have this new thing to share.</p><p>Here is one line of the oh-so-familiar code:</p><div
class="wp_syntax"><div
class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.old&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replaceWith</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.new&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.trash&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Deleted&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>If you got no idea what the line means, then let us be honest to each  other: How long have you been under the rock? Man, it&#8217;s <a
href="http://www.jquery.com">jQuery</a>! And the code I&#8217;ve just shown is a really simple stupid example about <a
href="http://docs.jquery.com/Manipulation">manipulation</a>!</p><p>Now, once you know what it is and what it does, take a look at this:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">pq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div.old'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">replaceWith</span><span style="color: #009900;">&#40;</span>pq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div.new'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.trash'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Deleted'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>You must have got it right: it&#8217;s PHP &#8211; <abbr
title="Object Oriented Programming">OOP</abbr> PHP 5 to be precise. But what was that with those jQuery stuffs in the middle, you ask?</p><p>Well, it&#8217;s <a
href="http://code.google.com/p/phpquery">phpQuery</a>. And it&#8217;s what I&#8217;m about to introduce here and now. <span
id="more-252"></span></p><p>As the name calls, phpQuery is a jQuery port to PHP. Being based on the most famous Javascript library, phpQuery provides server-side, chainable, CSS3 selector driven <abbr
title="Document Object Model">DOM</abbr> <abbr
title="Application Programming Interface">API</abbr>. Many things in jQuery are available/doable in phpQuery &#8211; including but not limited to selectors, attributes, traversing, manipulation, utilities, even AJAX and events. And the syntax is just about, if not most of the time exactly the same as that in jQuery &#8211; of course if that dollar character doesn&#8217;t have a special role in PHP. Example, here is a sample selector code:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">pq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.class ul &gt; li[rel='foo']:first:has(a)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Here is some traversing back and forth:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">pq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div &gt; p'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'div &gt; ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':has(a)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'p:first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nextAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">andSelf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>With some attributes here and there:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;">pq<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'href'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'newVal'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'className'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">html</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'newHtml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><p>Impressive, no? So what is it for? What are the benefits of using phpQuery?</p><p>A bunch!</p><ul><li>From the simplest, imagine you are &#8220;stealing&#8221; some content from a website. Imagine you will need to parse the content out from that garbage of (X)HTML, Javascript and CSS tags. Imagine you suck at Regular Expression. How will you reach the goal then? No worries with phpQuery. Just load the (X)HTML content into phpQuery using phpQuery::newDocument(X)HTML(), do some basic selector parsing like pq(&#8220;div#holder :text:first:not(&#8216;.bad-boy&#8217;)&#8221;), and then val(), and then that&#8217;s it. Well, what do you say?</li><li>To some server side DOM creator helpers, when you need to fully control the DOM.</li><li>To a jQuery plugin named <a
href="http://code.google.com/p/phpquery/wiki/jQueryServer">jQueryServer</a> that beautifully allows you to &#8220;1. Connect to server and make an Ajax request to somewhere (crossdomain allowed), 2. Do some manipulations, you can even trigger a server-side event, and 3. Get processed data back to the browser&#8221;. Have you imagined it? Have you? Woa, I&#8217;m excited!</li><li>And last but not least, to something much more advanced, a TemplatE SysteM &#8211; they do call it <a
href="http://tobiasz123.wordpress.com/category/projects/querytemplates/">QueryTemplates</a>. This time it really gets big!</li></ul><p>No, don&#8217;t tell me you&#8217;re not impressed!</p> <img
style='display:none' id="post-252-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://www.phoenixheart.net/2009/03/phpquery/',title:'phpQuery',tweet:'It&#8217;s been quite a while I don&#8217;t have any update on this blog (sorry my *ten* RSS subscri',description:'It&#8217;s been quite a while I don&#8217;t have any update on this blog (sorry my *ten* RSS subscri'})"><script type='text/javascript'>document.getElementById("post-252-blankimage").onload();</script>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/03/phpquery/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 5/11 queries in 1.415 seconds using disk

Served from: www.phoenixheart.net @ 2010-09-09 07:53:01 -->