Basics of phpQuery – jQuery style DOM selection in PHP

One of the biggest time saving inclusions in jQuery is the selection engine, that magical thing that lets you use normal CSS calls to select any elements on the dom. It’s great if your working with pages in javascript, but what if you want to do this is PHP, no such built in functionality exists (at least none that work as well), thats where phpQuery comes in.

PHP is a server DOM selection API based on jQuery and it works very similarly, being able to do all the same things that jquery can do, in php. It’s also much faster than trying to do it with regular expressions (due to both the time it takes to formulate all he regexs is needed, even if your using regexpal, and the time it takes for larger documents to process.

How it works

First thing your going to need to do it grab phpQuery, I suggest grabbing the onefile version here. Then include it into your existing file. After it’s included you’ll need to create a new phpQuery document like this:

$html would be the raw HTML you want to process, you can also load from as file using phpQuery::newDocumentFile('path/to/file'), The script will automatically determine the file type to use. If, for some reason, it’s trying to process a file in an incorrect for may or you want to want to process something in a different format, you can force the issue using one of the load functions here. phpQuery supports HTML, XHTML, XML, and PHP. Using the generic should be find, I’ve never had to try and process the file the wrong way.

After you have your new document set up all you need to do is make a call like this:

The only difference between a phpQuery call and a jQuery one is that instead of $() (or jQuery() if in safe mode) you use pq(). Anything inside those ()’s will be selected the same way.

But wait, Theres more

phpQuery can do more than just select elements in the dom, it can also be used get element attributes. Lets say you want to get the href of a link inside of out #this-awesome-div. You can do that by making some slight tweaks to our pq() call.

That will will give us the href of the link, so we can use it hoever we want. Again this is the same as jQuery, except naturally you using -> instead of . to refine the search. This attr can be used with any attribute, even arbitrary ones, just like in jQuery. You can even add additional attributes by passing a new key and value, want to make that link open in a new window? Try using attr('target','_blank') and it’ll be added for you.

To much for one post

That only scratches the surface of all the things phpQuery can do. It also supports making ajax calls (with cross domain support), events (with a few simple exceptions), looping and iteration function, and it even supports plugin functions (or at least, has plans to support them as only one seems to be implemented now). I’ve used it a couple time and havent played with all the features covered in the manual yet. That said I have created something at least a little useful with it. I have a script that will grab a random wallpaper from and save it to your disc (while moving the image in folder to the disk). Combine this is a nice cron event and a background changer and you could have a new, unique background image ever day (or hour if you want). You can see an example of it here, at the Random Background Image Selector example page (wow, thats a mouthful), if you want to play with it a bit I have the you can download the source here.

As always feel free to leave any questions or comments below or over on the relevant Google+ post, I’ll try to help as best I can with them (but like I said, I havnt used all of phpQuery’s functionality yet). This code is also up on Snipplr for those who use it (I’m open to other, additional services as well if you have any suggestions).

Side note, I know I’ve been posting less than I wanted to, I’ll probably be off for a week or two coming up hopefully soon here, not that it’ll be noticeable with how spotty postings been anyways.

Leave a Reply

Your email address will not be published. Required fields are marked *