I need to extract some data from a webpage with PHP. The part that I'm interested in is structured similarly to this:
<a href="somepath" target="fruit">apple</a>
<a href="somepath" target="animal">cat</a>
<a href="somepath" target="fruit">orange</a>
<a href="somepath" target="animal">dog</a>
<a href="somepath" target="fruit">mango</a>
<a href="somepath" target="animal">monkey</a>
First, I want to extract all fruits, and then all animals, so that I have them nicely grouped.
I figured out how to loop through all attribute values. Here's the code:
$dom = new DOMDocument();
$html = file_get_contents('example.html');
@$dom->loadHTML($html);
$a = $dom->getElementsByTagName('a');
for ($i; $i < $a->length; $i++) {
$attr = $a->item($i)->getAttribute('target');
echo $attr . "\n";
}
So I get:
fruit animal fruit animal fruit animal
I also found out how to get the elements' text content:
$a->item($i)->textContent
So, if included in loop and echoed, I get:
apple cat orange dog mango monkey
I feel like I'm very close, but I can't get what I want. I need something like this:
if (target = "fruit") then give me "apple, orange, mango".
How can I do it?