By Matt Butcher
QueryPath
QueryPath is a tool for manipulating HTML and XML documents in PHP using a chainable interface. It is similar to jQuery in that respect.

- Main page: http://querypath.org
- Documentation: http://api.querypath.org
- Wiki: https://fedorahosted.org/querypath
- Download: https://fedorahosted.org/released/querypath/
A typical QueryPath script looks something like this:
<?php require_once 'QueryPath/QueryPath.php'; qp(QueryPath::HTML_STUB)->find('body')->text('Hello World')->writeHTML(); ?>
The first line simply requires the library.
The second line opens an HTML stub document (which contains the basic HTML you would need to create a new document), navigates the document's body, and then sets the title to Hello World. Finally, the full HTML is written to the browser.
Here's a more complex QueryPath routine, this time showing several of the QueryPath methods:
<?php require_once 'QueryPath/QueryPath.php'; // Begin with an HTML stub document (XHTML, actually), and navigate to the title. qp(QueryPath::HTML_STUB, 'title') // Add some text to the title ->text('Example of QueryPath.') // Now look for the <body> element ->find(':root body') // Inside the body, add a title and paragraph. ->append('<h1>This is a test page</h1><p>Test text</p>') // Now we select the paragraph we just created inside the body ->children('p') // Add a 'class="some-class"' attribute to the paragraph ->attr('class', 'some-class') // And add a style attribute, too, setting the background color. ->css('background-color', '#eee') // Now go back to the paragraph again ->parent() // Before the paragraph and the title, add an empty table. ->prepend('<table id="my-table"></table>') // Now let's go to the table... ->find('#my-table') // Add a couple of empty rows ->append('<tr></tr><tr></tr>') // select the rows (both at once) ->children() // Add a CSS class to both rows ->addClass('table-row') // Now just get the first row (at position 0) ->eq(0) // Add a table header in the first row ->append('<th>This is the header</th>') // Now go to the next row ->next() // Add some data to this row ->append('<td>This is the data</td>') // Write it all out as HTML ->writeHTML(); ?>
On this blog, I will occasionally write about various uses of QueryPath.








Recent comments
8 hours 25 min ago
1 day 2 hours ago
1 day 5 hours ago
1 day 19 hours ago
1 day 22 hours ago
1 day 22 hours ago
2 days 3 hours ago
2 days 20 hours ago
2 days 20 hours ago
3 days 3 hours ago