It’s been quite a while I don’t have any update on this blog (sorry my *ten* RSS subscribers…). If there is any excuse, then it should be the fact that I’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.

Ok cut the chit-chat, today I have this new thing to share.

Here is one line of the oh-so-familiar code:


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’s jQuery! And the code I’ve just shown is a really simple stupid example about manipulation!

Now, once you know what it is and what it does, take a look at this:


You must have got it right: it’s PHP - OOP PHP 5 to be precise. But what was that with those jQuery stuffs in the middle, you ask?

Well, it’s phpQuery. And it’s what I’m about to introduce here and now.

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 DOM API. Many things in jQuery are available/doable in phpQuery - 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 - of course if that dollar character doesn’t have a special role in PHP. Example, here is a sample selector code:

<code>pq(".class ul > li[rel='foo']:first:has(a)");</code>

Here is some traversing back and forth:

<code>pq('div > p')-&gt;add('div > ul')-&gt;filter(':has(a)')-&gt;find('p:first')-&gt;nextAll()-&gt;andSelf();</code>

With some attributes here and there:

<code>pq('a')-&gt;attr('href', 'newVal')-&gt;removeClass('className')-&gt;html('newHtml');</code>

Impressive, no? So what is it for? What are the benefits of using phpQuery?

A bunch!

  • From the simplest, imagine you are “stealing” 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(“div#holder :text:first:not(‘.bad-boy’)”), and then val(), and then that’s it. Well, what do you say?
  • To some server side DOM creator helpers, when you need to fully control the DOM.
  • To a jQuery plugin named jQueryServer that beautifully allows you to “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”. Have you imagined it? Have you? Woa, I’m excited!
  • And last but not least, to something much more advanced, a TemplatE SysteM - they do call it QueryTemplates. This time it really gets big!

No, don’t tell me you’re not impressed!

You can follow any responses to this entry through the RSS 2.0 feed.