Preg match where id attribute changes by 1
I want to get the innertext of each anchor. And then print the results. The "ctl" in the id attribute is increased by 01 each time though.
I have to match them by the id attribute be开发者_如何学Ccause of the page these anchors are located.
How could I do that?
<a id="ctl00_mainContent_rpLeaderboard_ctl01_hypServiceRecord" href="/Stats/Reach/default.aspx?player=DJ+DarkRecon">DJ DarkRecon</a>
<a id="ctl00_mainContent_rpLeaderboard_ctl02_hypServiceRecord" href="/Stats/Reach/default.aspx?player=X+PR+Legacy+X">X PR Legacy X</a>
<a id="ctl00_mainContent_rpLeaderboard_ctl03_hypServiceRecord" href="/Stats/Reach/default.aspx?player=Forgiver2">Forgiver2</a>
Here is a rather quick solution using an HTML parser:
$dom = new DomDocument;
$dom->loadHTML('
<!DOCTYPE html>
<a id="ctl00_mainContent_rpLeaderboard_ctl01_hypServiceRecord" href="/Stats/Reach/default.aspx?player=DJ+DarkRecon">DJ DarkRecon</a>
<a id="ctl00_mainContent_rpLeaderboard_ctl02_hypServiceRecord" href="/Stats/Reach/default.aspx?player=X+PR+Legacy+X">X PR Legacy X</a>
<a id="ctl00_mainContent_rpLeaderboard_ctl03_hypServiceRecord" href="/Stats/Reach/default.aspx?player=Forgiver2">Forgiver2</a>
');
$i = 1;
while($i < 4) {
var_dump($dom->getElementbyId("ctl00_mainContent_rpLeaderboard_ctl0{$i}_hypServiceRecord")->nodeValue);
$i++;
}
There are a numerous ways to go about this via a parser, I'm hoping this will give you some sort of a start.
How about
preg_match_all('~<a[^>]*>([^<]*)</a>~i', $text);
Of course, that works just in the easy example you've given. For a more complex html problem, regex isn't the best answer.
EDIT:
preg_match_all('~<a id="([^"]*)"[^>]*>([^<]*)</a>~i', $text);
If you need the id too.
精彩评论