My first programming language was javascript, and the book that made me first realize that I wannted to program for a living was DOM scripting from publisher Friends of ED. Back then I thought javascript was all about DOM manipulation, and honestly, back then it was. Now, javascript has become a more respected language and has found its place as the king on the realm of client side programming, and it has made its way to the server end. But DOM manipulation is still a need. For better or worse, the DOM doesn’t seem to be going away any time soon.

Just this week I’ve been working with the DOM, but this time in php. So how is it like to work with the DOM in php? While this is not my first time working with the DOM in php, I must say that it feel wierd. I think PHP + DOM is not a natural combination, but it works well none the less.

When you want to work with the DOM in PHP, you have a few options. I’ve found that the PHP library is enough, but maybe not the most comfortable way. It is what I use anyway, but that is because I only needed to interact with the DOM very little, if you need heavy DOM interaction in PHP, I would recommend you look at some of these projects:

htmlSQL: htmlSQL is a very simple php class that makes it really easy to navigate the DOM using SQL-like syntax. The project has been abandoned, but if you just need to do some interaction with the DOM, it should be enoug. Optionally, if you really like the project, you could fork it and continue its development.

Simple HTML DOM: This is another options. It follows a more OOP approach to navigating the DOM, and it has fetures that the PHP DOM library lacks, like the hability to pass in a URL and get the DOM representation of that resource. If you want to do that with the PHP DOM library, you would need to first fetch the document using something like cURL, and then load the HTML into the DOM object.

PHPQuery: This is probably the most interesting project. It is ispired by jQuery, so if you are familiar with jQuery, you will love phpQuery. Do not expect, however, to find function like fade or animate, they would not make sense on the server side where there is no visual representation of the DOM. You can ofcourse make animations on the server side, but that would just be silly.

Next time I will show you how to build a simple web scraper with php, and we will use these libraries, and the PHP DOM library and see which one is the easiest to work with.


2 thoughts on “DOM and PHP

Comments are closed.