A (very) simple example of parsing HTML document or string with PHPQuery

December 16th, 2013 by Alex Leave a reply »

So you just downloaded the PHPQuery library and wonder how to get it up and working. It is actually very simple:

require_once 'phpquery.php';
// if we have just a string, then we need to make a document out of it
$html = '<html><head></head><body>'.$my_html_string.'</body></html>';
// or if you have a url, then you can just load it like this
// $html = file_get_contents('http://example.com');
phpQuery::newDocumentXHTML($html);
 
// output all links in this document
foreach(pq('a') as $a) {
  echo pq($a)->attr('href') . '<br />';
}

1 comment

  1. PureLogic says:

    Thank you! That was fabulous 🙂