Created
December 12, 2011 13:41
-
-
Save reinink/1467201 to your computer and use it in GitHub Desktop.
Example of how to parse HTML document with phpQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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