Created
November 7, 2013 05:50
-
-
Save tomkel5/7349669 to your computer and use it in GitHub Desktop.
Example of Symfony Crawler
This file contains hidden or 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 | |
require_once 'vendor/autoload.php'; | |
use Symfony\Component\DomCrawler\Crawler; | |
$html = '<!DOCTYPE html> | |
<html> | |
<body> | |
<table border="0" cellpadding="0" cellspacing="1"> | |
<tr> | |
<td width="110" class="lightLink">Last Online</td> | |
<td>11 hours ago</td> | |
</tr> | |
<tr> | |
<td class="lightLink">Gender</td> | |
<td>Not specified</td> | |
</tr> | |
<tr> | |
<td class="lightLink">Birthday</td> | |
<td>Some Date</td> | |
</tr> | |
<tr> | |
<td class="lightLink">Location</td> | |
<td>California, USA</td> | |
</tr> | |
<tr> | |
<td class="lightLink">Website</td> | |
<td><a href="http://www.example.net" target="_blank">www.example.net</a></td> | |
</tr> | |
<tr> | |
<td class="lightLink">Join Date</td> | |
<td>March 5, 2012</td> | |
</tr> | |
<tr> | |
<td class="lightLink">Access Rank</td> | |
<td>Member</td> | |
</tr> | |
<tr> | |
<td class="lightLink">Anime List Views</td> | |
<td>432</td> | |
</tr> | |
<tr> | |
<td class="lightLink">Manga List Views</td> | |
<td>340</td> | |
</tr> | |
</table> | |
</body> | |
</html> | |
'; | |
$crawler = new Crawler($html); | |
// Either of these work: | |
// $crawler = $crawler->filterXPath("//table/tr"); | |
// $crawler = $crawler->filter("table > tr"); | |
$crawler = $crawler->filter("table > tr"); | |
$nodeValues = $crawler->each( | |
function (Crawler $node, $i) { | |
$first = $node->children()->first()->text(); | |
$last = $node->children()->last()->text(); | |
return array($first, $last); | |
} | |
); | |
print_r($nodeValues); |
This file contains hidden or 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
Array | |
( | |
[0] => Array | |
( | |
[0] => Last Online | |
[1] => 11 hours ago | |
) | |
[1] => Array | |
( | |
[0] => Gender | |
[1] => Not specified | |
) | |
[2] => Array | |
( | |
[0] => Birthday | |
[1] => Some Date | |
) | |
[3] => Array | |
( | |
[0] => Location | |
[1] => California, USA | |
) | |
[4] => Array | |
( | |
[0] => Website | |
[1] => www.example.net | |
) | |
[5] => Array | |
( | |
[0] => Join Date | |
[1] => March 5, 2012 | |
) | |
[6] => Array | |
( | |
[0] => Access Rank | |
[1] => Member | |
) | |
[7] => Array | |
( | |
[0] => Anime List Views | |
[1] => 432 | |
) | |
[8] => Array | |
( | |
[0] => Manga List Views | |
[1] => 340 | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment