php - Retrieve the number of 'a' tags between two tags -
what want retrieve number of html <a> tags between specific <td> tag.
the illustration have don't know how set rest in code..
$dom = new domdocument(); $dom->loadhtml($text); $i = 0; foreach($dom->getelementsbytagname("td") $node){ //retrieve every td tag have attribute bgcolor = #0051ab //<td bgcolor=#0051ab> nodevalue </td> if($node->getattribute("bgcolor") == "#0051ab"){ $cat[]= $node->nodevalue; } //here identify every 'a' html tag between $node , next one!! //<a href="path">nodevalue</a> } example
<table><tr><td bgcolor=#0051ab>project 1</td></tr></table> <a>link1</a> other tags , text.. <a>link 2</a> come in code here <table><tr><td bgcolor=#0051ab>project 2</td></tr></table> codecodecode <a>link3</a> codecodecode the result need: (0 = name of td nodevalue, 1 = number of tags before next node)
array => ( array[0] => ([0] => project1, [1] => 2 ), array[1] => ([0] => project2, [1] => 1 ) ) thanks advice.
i'll prefer querypath requirement on php dom; why? it's different discussion.
below solution of problem.
download querypath , include in php file.
require("../../querypath\querypath.php"); following illustration html parsing
$text="<body> <table><tr><td bgcolor=#0051ab>project 1</td></tr></table> <a>link1</a> other tags , text.. <a>link 2</a> come in code here <table><tr><td >project 2</td></tr></table> codecodecode <a> should not included</a> codecodecode <table><tr><td bgcolor=#0051ab>project 2</td></tr></table> codecodecode <a>link3</a> codecodecode</body>"; code parse html
$tags=htmlqp($text,'body')->children(); $isrequiredtag=false; $i=0; foreach($tags $pr) { $tag= $pr->tag(); if($tag=='table'){ $isrequiredtag= (htmlqp($text,$tag)->eq($i)->find('td')- >attr('bgcolor')=='#0051ab')?"true":"false"; $i++; } if ($isrequiredtag=="true" && $tag=='a') echo $pr->text(); } php dom tags
No comments:
Post a Comment