Skip to content
This repository

QueryPath is a PHP library for manipulating XML and HTML. It is designed to work not only with local files, but also with web services and database resources.

Octocat-spinner-32 bin Minor XML fix.
Octocat-spinner-32 examples Issue #132: Removed deprecated :root pseudoclass.
Octocat-spinner-32 patches Cleaing up get tree.
Octocat-spinner-32 phar Fixed a bug in the phar builder (Issue #43).
Octocat-spinner-32 src Update wrap(), wrapInner(), and wrapAll()
Octocat-spinner-32 test Update DOMQuery::prepareInsert()
Octocat-spinner-32 tutorials Addressed bug in my implementation of :contains(), added contains-exa…
Octocat-spinner-32 .gitignore Updated gitignore.
Octocat-spinner-32 API Updated release notes.
Octocat-spinner-32 COPYING-LGPL.txt New Phing-based build system.
Octocat-spinner-32 COPYING-MIT.txt Updated license, INSTALL, and README.
Octocat-spinner-32 CREDITS Update CREDITS
Octocat-spinner-32 INSTALL Updated INSTALL and README docs.
Octocat-spinner-32 LGPL-2.1.txt Added licensing files.
Octocat-spinner-32 Makefile Integrating the new DOMTraverser with DOMQuery.
Octocat-spinner-32 README.md Update README.md
Octocat-spinner-32 RELEASE Update for issue #128
Octocat-spinner-32 build.xml Updated build.xml to point to current pyrus.
Octocat-spinner-32 composer.json Make composer autoloader include qp() functions
Octocat-spinner-32 config.doxy Fixed version number in doxy config.
Octocat-spinner-32 package.xml Fixed build system for template-based PEAR builds with Pyrus.
Octocat-spinner-32 package_compatible.xml Fixed a bug in the phar builder (Issue #43).
Octocat-spinner-32 pear-summary.txt Added a file that contains PEAR summary text.
Octocat-spinner-32 quickstart-guide.md Added QuickStart guide.
README.md

QueryPath: Find your way.

New development is happening on the 3.x branch.

Authors: Matt Butcher (lead), Emily Brand, and many others

Website | API Docs | VCS and Issue Tracking | Support List | Developer List | Pear channel |

This package is licensed under an MIT license (COPYING-MIT.txt).

At A Glance

QueryPath is a jQuery-like library for working with XML and HTML documents in PHP.

Say we have a document like this:

<?xml version="1.0"?>
<table>
  <tr id="row1">
    <td>one</td><td>two</td><td>three</td>
  </tr>
  <tr id="row2">
    <td>four</td><td>five</td><td>six</td>
  </tr>
</table>

And say that the above is stored in the variable $xml. Now we can use QueryPath like this:

<?php
// Get all of the <td> elements in the document and add the
// attribute `foo='bar'`:
qp($xml, 'td')->attr('foo', 'bar');

// Or print the contents of the third TD in the second row:
print qp($xml, '#row2>td:nth(3)')->text();

// Or append another row to the XML and then write the 
// result to standard output:
qp($xml, 'tr:last')->after('<tr><td/><td/><td/></tr>')->writeXML();

?>

(This example is in examples/at-a-glance.php.)

With over 60 functions and robust support for chaining, you can accomplish sophisticated XML and HTML processing using QueryPath.

QueryPath Installers

The preferred method of installing QueryPath is via Composer.

You can also download the package from GitHub.

Older versions of QueryPath 2.x are still available from the PEAR repository.

Composer (Preferred)

To add QueryPath as a library in your project, add this to the 'require' section of your composer.json:

{
  "require": {
    "querypath/QueryPath": ">=3.0.0"
  }
}

The run php composer.phar install in that directory.

To stay up to date on stable code, you can use dev-master instead of >=3.0.0.

Pear

This is not supported any longer.

To install QueryPath 2.x as a server-wide library, you may wish to use PEAR or Pyrus. See pear.querypath.org for more information, or simply run these commands:

$ pear channel-discover pear.querypath.org
$ pear install querypath/QueryPath

Manual

You can either download a stable release from the GitHub Tags page or you can use git to clone this repository and work from the code. master typically has the latest stable, while 3.x is where active development is happening.

Including QueryPath

As of QueryPath 3.x, QueryPath uses the Composer autoloader if you installed with composer:

<?php
require 'vendor/autoload.php';
?>

If you installed QueryPath as a PEAR package, use it like this:

<?php
require 'QueryPath/qp.php';
?>

From the download or git clone:

<?php
require 'QueryPath/src/qp.php';
?>

QueryPath can also be compiled into a Phar and then included like this:

<?php
require 'QueryPath.phar';
?>

From there, the main functions you will want to use are qp() (alias of QueryPath::with()) and htmlqp() (alias of QueryPath::withHTML()). Start with the API docs.

Something went wrong with that request. Please try again.