get HTML element by attribute value in php

你说的曾经没有我的故事 提交于 2019-12-20 11:00:34

问题


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".

Can someone please point me in the right direction?

Thanks.


回答1:


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.




回答2:


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.




回答3:


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);
}


来源:https://stackoverflow.com/questions/8395523/get-html-element-by-attribute-value-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!