Make two array
$fruits=array();
$animals=array();
t and in loop when you get .
if(target=='fruit') {
array_push($fruits,$valueofelement);
} else if ($target=='animal') {
array_push($animals,$valueofelement);
}
use DOMXPath
and queries:
$doc = new DOMDocument();
$doc->Load('yourFile.html');
$xpath = new DOMXPath($doc);
$fruits = $xpath->query("//a[@target='fruit']");
foreach($fruits as $fruit) {
// ...
}
$animals = $xpath->query("//a[@target='animal']");
foreach($animals as $animal) {
// ...
}
See this demo.
Just continue
on target
attributes which aren't fruit
, and then add the textContent
of the elements to an array.
$nodes = array();
for ($i; $i < $a->length; $i++) {
$attr = $a->item($i)->getAttribute('target');
if ($attr != 'fruit') {
continue;
}
$nodes[] = $a->item($i)->textContent;
}
$nodes
now contains all the nodes of the elements which have their target
attribute set to fruit
.