New release - phpQuery 0.9.5 RC2

Second Release Candidate is available for download. There are couple of reasons for not marking it as stable, although this version is really a candidate and will hopefully became stable without any modifications.

Besides many bugfixes, there are also some new features in this release, but those are only significant ones.


  • Mutation events New DOMNodeInserted and DOMNodeRemoved, part of W3C standards, are now available and automatically triggered.
  • getJSON method This really important method (from mashup point of view) is now available and supports JSONP datasources.
  • data() methods Internal datastore per node allows easier manipulation and data exchange.
  • Charset conversion for HTML jbondc commited a patch to convert charset during document load (using mbstring). Right now it works only for HTML, other cases are scheduled for v1.1.

PEAR server

PEAR installation is finally available. It's the fastest and most comfortable method to install phpQuery system-wide. Just type those 2 commands as privileged user:

pear channel-discover phpquery-pear.appspot.com
pear install phpquery/phpQuery

After that only thing you need to do is:

require "phpQuery.php";

Linux packages

Both DEB and RPM packagages are now available. Those packages doesn't interfere with PEAR package.


As project almost reached it's first stable release and it's quite feature-complete, i've decided to open it for donations. If you're using it in your own project, maybe planning to do it with next one, waiting for one of many planned features or just like the idea of more JavaScript-like server-side, please consider 10$ donation to support phpQuery development. All amounts are appreciated!


Since some time you can follow phpQuery development on Twitter. Posted messages contain such informations as new commits (with log), new issues, comments on issues and also this blog posts. All of them are directly linked to proper URL.

Full Changelog

  • added data() and removeData()
  • fixed hardcoded charsets in contentTypes
  • fixed $context as array in find()
  • fixed PHP 5.3 closure compatibility in filter()
  • fixed filter callback result interpretation
  • fixed queries when stack was XML root
  • fixed sibling methods for textnodes
  • fixed attr() with empty stack
  • fixed redundant phpQuery::$documents bindings
  • fixed phpQuery::callbackRun() in PHP 5.3
  • filter() detects instances of Callback class
  • fix for proper query roots
  • small fix in google_login script
  • improved dumpTree()
  • fixed loading HTML with nested xml namespace declaration
  • fixed parseJSON() encoding issues
  • added getJSON() with JSONP support
  • added WebBrowser per event callback support
  • added phpQuery::extend
  • added __callStatic for phpQuery class static plugins namespace (PHP 5.3)
  • fixed document references in phpQuery::getDocumentID
  • fixed DOMNode as first parameter in pq();
  • added DOMNodeInserted and DOMNodeRemoved mutation events
  • not() fixed for set of elements
  • fixed trigger() static call error
  • added named callbacks
  • fixed val() for selects
  • newInstance() supports raw markup
  • new calback types
  • added HTML load-time charset convertion (patch from jbondc)

Get newest version from download page.


New release - phpQuery 0.9.5 RC

After quite long while after 0.9.5 Beta4, Release Candidate has been released. It brings couple of bugfixes and generally should be considered as most stable available for today.

SVN repository has been reorganized a bit - releases has been tagged and trunk doesn't contain full Zend Framework as svn:external anymore, so checkouts should be more robust.

Linux packages are postponed until 0.9.5 final release. Besides .rpm package planned is .deb one and straightforward general linux installation howto.

Next releases will focus on jQuery 1.3 compatibility.

Changes since 0.9.5 Beta4:
  • fixed [attr$=ending] selector for mb_string
  • readded existing DOMDocument objects integration
  • added wrapInnerPHP
  • fixed empty return from phpQuery::$plugins namespace
  • reading pages without HTML tag with doctype as not fragments
  • #84: text() will not set text to an empty string or ""0""
  • fixed prev() & next() for set of elements
  • attr() returns null when no attr is set
  • fixed val() for set of elements with select
  • :contains() pseudoclass is now case-insensitive
  • fixed val() and unicode string comparsion

Get it from download section.


QueryTemplates - templating engine using phpQuery

Introducing QueryTemplates, a completely new templating engine based on phpQuery.

Quote from project page:

PHP based templating engine creating reusable native templates in various languages.

As for today, supported are following sources: HTML, XML, XHTML, PHP and PHP callbacks which can be processed into PHP or JavaScript template file.

Library uses popular web 2.0 pattern load-traverse-modify thou jQuery like chainable API and provides developer several rapid template filling methods.

Using QueryTemplates developer is independent from designer. He loads pure markup HTML/XML file(s), traverses using CSS selectors and modifies it, injecting data and logic.

Example code:
    ->find('ul > li')
      ->loopOne('data', 'row')
        ->varsToSelector('row', $rowFields)
Library uses advanced phpQuery features like
  • inheritance
  • callbacks
  • plugins
There are many working examples of such templates on official project page.


New release - phpQuery 0.9.5 Beta4

The fourth beta is mostly bugfix release and hopefully it's the last one before RC. Two most noticeable fixes are multibyte characters support (for queries and DOM structure) and fixed zip release package, which couldn't be extracted on case-insensitive filesystems.

The is also new release package type - All-in-One. It's for those who prefer one consistent file. Very important thing to notice - this package doesn't include Zend Framework, used for HTTP Client. It means You have to have one on you own to use Ajax.

Release is also available as RPM package - normal and All-in-One version.


• fixed filename collision (win & macos)
• fixed "> .class" selector
• fixed :not(selector) pseudoclass when selector is a pseudoclass
• fixed attribute matching in filter()
• fixed Ajax lowercased request method
• fixed selector filter in nextAll() and prevAll()
• added mbstring extension compatibility
• added context guessing when passing DOMNode to pq() as only param
• added downloadTo() method for WebBrowser
• added dash "-" as allowed tag character
• added phpQuery::$defaultDoctype
• added contentsUnwrap()
• added switchWith()
• get() now use callbacks for each returned element separately
• fixed foreach iteration
• fixed parents() with $selector
• fixed parents() for documentFragment
• fixed val() with $val == array
• fixed val() for select
• fixed val(0)
• fixed unloadDocuments()
• added fix_webroot script
• fixed change event triggering
• added change event for textarea
• fixed isHTML()
• fixed filter() and not() with multiply selectors and pseudoclasses

Get phpQuery-0.9.5 Beta4 from Download section.


Basic example - demo.php

This example shows how to use basic functionality of phpQuery, such as:
  • Document intialization
  • Querying document
  • Filling with markup
  • Manipulating DOM
  • Selecting documents
  • Iterating with foreach
  • Printing output
Example file can also be found in all release packages.

// $doc = phpQuery::newDocumentHTML($markup);
// $doc = phpQuery::newDocumentXML();
// $doc = phpQuery::newDocumentFileXHTML('test.html');
// $doc = phpQuery::newDocumentFilePHP('test.php');
// $doc = phpQuery::newDocument('test.xml', 'application/rss+xml');
// this one defaults to text/html in utf8
$doc = phpQuery::newDocument('<div>');

// array syntax works like ->find() here
// array set changes inner html
$doc['div ul'] = '<li>1</li><li>2</li><li>3</li>';

// almost everything can be a chain
$doc['ul > li']
// save it anywhere in the chain

// pq(); is using selected document as default
// documents are selected when created, iterated or by above method
// query all unordered lists in last selected document

// all LIs from last selected DOM
foreach(pq('li') as $li) {
// iteration returns PLAIN dom nodes, NOT phpQuery objects
$tagName = $li->tagName;
$childNodes = $li->childNodes;
// so you NEED to wrap it within phpQuery, using pq();

// 1st way
print phpQuery::getDocument($doc->getDocumentID());
// 2nd way
print phpQuery::getDocument(pq('div')->getDocumentID());
// 3rd way
print pq('div')->getDocument();
// 4th way
print $doc->htmlOuter();
// 5th way
print $doc;
// another...
print $doc['ul'];


phpQuery 0.9.5 Beta2 released

New major version is out! After much of rewriting old DOMDocument releated code, public release is avaible for download. New code parts fixes many previous problems, but not only bugfixes cames with this version.


  • New callback system More flexible callbacks allows you to control passed parameter order / values and also there is CallbackReference, which passes callback parameter straight to reference. You can read more about callbacks in almost-up-to-date wiki page or see some code in action here.
  • Console (CLI) interface One of my favorite features, allowing web scraping right from console. You can see some examples here. There is also RPM package available.
  • XML, XHTML and PHP files support There are dedicated methods for loading all type of documents (HTML, XHTML, XML) and one for autodetected content. Everything basses on content-type header. For PHP files there is newDocumentPHP(), which works like newDocument() (it needs content-type when auto-detection isn't possible) and converts PHP tags to <php>, which later can be exported using $pq->php(), just like html() method :)
  • New Scripts plugin This plugin simplifies writing short code scripts which can be easily reused (chained). In latest release package there are 2 scripts: google_login and safe_print. More about the plugin in this post.

Full changelog

  • added extended callback support in ALL methods (yay!)
  • added CLI interface
  • added Scripts plugin
  • added bootstrap file support
  • DOMDocument wrapper introduced (alpha)
    • fixed inserts/appends
    • added XML/XHTML compatibility
    • contentType support
  • phpQuery
    • ::newDocument($markup, $contentType = null) // new document from markup, type
    • autodetected
    • ::newDocumentHTML($html) // new document, type HTML
    • ::newDocumentXML($xml) // new document, type XML
    • ::newDocumentXHTML($xml) // new document, type XHTML
    • ::newDocumentPHP($markup, $contentType)
    • ::newDocumentFile($file)
    • ::newDocumentFileHTML($file)
    • ::newDocumentFileXML($file)
    • ::newDocumentFileXHTML($file)
    • ::newDocumentFilePHP($file)
  • $pq
    • ->isXML()
    • ->isXHTML()
    • ->isHTML()
    • ->html()
    • ->xml()
    • ->htmlOuter()
    • ->xmlOuter()
  • new test documents
  • class reorganization
  • jQueryServer
    • config file
    • allowed referers list
    • using Json straight from phpQuery
  • fixed ajaxAllowURL
  • XHTML fixes
  • fixed notice for DOMTextNode
  • added callbacks in text()
  • added callbacks in htmlOuter()
  • fixed text($text)
  • added getDocument()
  • fixed html() with text node at beggining
  • fixed zend framework selection in releases (missing some cookie dependencies)
  • fixed returns in phpQuery::$plugins
  • fixed $callback in .submit()
  • added phpQuery::plugin() and $pq->plugin() methods
  • fixed many minor issues in WebBrowser plugin
  • fixed :pseudoclass:pseudoclass selector
  • added CallbackReference
  • fixed include paths on win32
  • fixed some events triggers
Get latest release from project's Download Page and report bugs to Issue Tracker.


Wiki has documentation

Project's Wiki has been created and now contains all method names from jQuery Documentation Site. Whole process has been automated using docs-import script, which fetches data from this XML, an effect of query-api-browser project (thx guys). Here's some example snippet - Selectors section.

$categories = $pq->find('cat[value=Selectors] subcat');
$content = array();
      foreach($categories as $subcat) {
          $content[] = '===='.pq($subcat)->attr('value')."====";
          foreach(pq($subcat)->find('selector') as $selector)
              $content[] = ' * *`'.pq('sample', $selector)->text().'`* '.pq('> desc', $selector)->text();

As you can see Wiki markup is automatically applied and whole task is quite easy using jQuery-like interface. Wiki documentation can be usefull due to official documentation going offline quite often lately. There is also great CHM version to download which i recommend.

Besides coping documentation ;) there are some pure phpQuery-specific pages like PHPSupport or Callbacks. More will come.