Skip to content

Instantly share code, notes, and snippets.

@reinink
Created December 12, 2011 13:41
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save reinink/1467201 to your computer and use it in GitHub Desktop.
Save reinink/1467201 to your computer and use it in GitHub Desktop.
Example of how to parse HTML document with phpQuery
<?php
// Include the phpQuery library
// Download at http://code.google.com/p/phpquery/
include 'phpQuery.php';
// Load Mike Fisher's player page on thescore.com
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.thescore.com/nhl/player_profiles/859-mike-fisher');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
// Create phpQuery document with returned HTML
$doc = phpQuery::newDocument($html);
// Create array to hold stats
$stats = array();
// Add stats from page to array
// Notice the CSS style selector
foreach($doc['#career_stats_regular table tr:nth-child(1) td'] as $td)
{
$stats[] = pq($td)->html();
}
// Display stats
print_r($stats);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment