A few weeks ago, I created a free PHP library for traversing (X)HTML documents: PQLite (PHP Query Lite). In this article, I will present a few key features of PQLite and how easy it is to use.
Before I begin, what does it truly mean to traverse an (X)HTML document?
- Being able to sift through HTML to find certain tags
- Applying the same concept to CSS selectors (classes, ids, etc.)
- Modifying these tags by changing their text and/or attributes
- Accessing special tags (odd, even, first child, last child, children of a parent tag) with simple functions
Let’s take a look at how PQLite works to achieve the above standards.
Initializing a PQLite Object
PQLite is initially created with HTML markup that is passed to it. This can be inline markup or even from a file. For example:
$PQLite = new PQLite( '<html><body><div id="example"><p></p></div></body></html>' );
The above creates a PQLite object with some basic HTML. This HTML can be reaccessed through
$PQLite->get_text(). When modifications occur, PQLite stores them internally and lets you access the changed code through this same function.
Applying a simple modification
To apply modifications, we first need to tell PQLite what tag we want to find. In this case, why don’t we find the inner paragraph element. To do so, let’s use a simple tag selector and PQLite’s
f( $str ) function:
$PQLite->f( 'p' );
The above function will take in a String parameter that contains almost any CSS selector. It will then return the tag found as an object that can be modified through more functions. Here are a few examples of what we can now do:
// Set the paragraph's inner text $PQLite->f( 'p' )->set_inner_text( 'This is a simple PQLite example' ); // Add a class to the paragraph $PQLite->f( 'p' )->set_attr( 'class', 'paragraph' ); // Completely change the paragraph $PQLite->f( 'p' )->set_outer_text( '<a href="http://pqlite.com'>This paragraph has been changed to a link element</a>' );
In those three simple statements, we changed the inner text, adjusted an attribute, and changed the outer text. What could make it even easier? How about chaining all those statements into one?
$PQLite->f( 'p' )->set_inner_text( 'This is a simple PQLite example' )->set_attr( 'class', 'paragraph' )->set_outer_text( '<a href="http://pqlite.com'>This paragraph has been changed to a link element</a>' );
Don’t forget that you can specify multiple selectors into the
f( $str ) function! All of the following will access the same paragraph (and these don’t even use classes):
// multiple ways to find the same element $PQLite->f( 'p' ); $PQLite->f( 'div p' ); $PQLite->f( '#example p' ); $PQLite->f( 'div#example p' ); $PQLite->f( 'body div#example p' ); $PQLite->f( 'html body div#example p' );
That was pretty nice, right?
Getting information from a document
Well, we can set modifications easily. How about doing the opposite – getting information?
// This will print 'paragraph' echo $PQLite->f( 'p' )->set_attr( 'class', 'paragraph' )->get_attr( 'class' );
The above example showed you the use of
get_attr( $attr ). Note that there are much more than just this one function:
// Print the paragraph's inner text echo $PQLite->f( 'p' )->get_inner_text(); // Print the outer text echo $PQLite->f( 'p' )->get_outer_text();
Consider reading more about these functions here.
Print out the finished HTML
Now all that’s left is to print out the final HTML. This can be done in two simple ways:
// If you don't need to get the text in a variable, you can print out the object itself echo $PQLite; // If you need the text or you just want to be consistent, use echo $PQLite->get_text();
The second method will let you store the text in a variable if necessary.
That’s all there is to a very brief example of PQLite and some of its features. If you want to read more about it, please visit the main page, documentation section, or download area.
It took me a while to come up with and create this project. Please tell me what you think about it. I would be happy to get any feedback!
In addition, please visit my blog on PQLite, called Resource Mania, for all the latest web resources. If you are a subscriber at Lateral Code, I would be grateful if did the same at Resource Mania! Thank you.