通过php中的属性值获取HTML元素 [英] get HTML element by attribute value in php

查看:107
本文介绍了通过php中的属性值获取HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从php的网页中提取一些数据。我感兴趣的部分的结构类似于:

 < a href =somepathtarget =fruit >苹果< / A> 
< a href =somepathtarget =animal> cat< / a>
< a href =somepathtarget =fruit> orange< / a>
< a href =somepathtarget =animal> dog< / a>
< a href =somepathtarget =fruit>芒果< / a>
< a href =somepathtarget =animal> monkey< / a>

首先,我要提取所有的水果,然后是所有的动物,以便我把它们分组好



我想出了如何循环遍历所有的属性值。以下是代码:

  $ dom = new DOMDocument(); 
$ html = file_get_contents('example.html');

@ $ dom-> loadHTML($ html);

$ a = $ dom-> getElementsByTagName('a'); ($ i; $ i< $ a-> length; $ i ++)

{
$ attr = $ a-> item($ i) - > getAttribute '目标');

echo $ attr。 \\\
;
}

所以我得到:



水果动物果实动物水果动物

我还发现如何获取元素的文本内容:

  $ a-> item($ i) - > textContent 

所以,如果包含在循环和回显中,我得到:

 苹果猫橙色狗芒果猴

我觉得就像我很接近,但是我无法得到我想要的东西。我需要这样的东西:



if(target =fruit)然后给我苹果,橙子,芒果。



有人可以指出我正确的方向吗?



谢谢。

解决方案 c c c c c c c c / code>,然后将元素的 textContent 添加到数组。

 $ code $ $ node = array(); ($ i; $ i< $ a-> length; $ i ++)

{
$ attr = $ a-> item($ i) - > getAttribute '目标');

if($ attr!='fruit'){
continue;
}

$ nodes [] = $ a-> item($ i) - > textContent;
}

$ nodes now包含元素的所有节点,它们的目标属性设置为 fruit


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.

解决方案

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.

这篇关于通过php中的属性值获取HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆