按属性名称读取子元素

时间:2014-05-15 21:49:26

标签: php dom domdocument

我无法弄清楚如何读取标签名称相同的多个子标签(如div)以及何时我想通​​过属性读取它。

所以我的html代码段看起来像这样:

<div>....</div>
<div>....</div>
<div class = 'iwantthisone'>
    <h4>value</h4>
    <div class ='ilikethistoo'>
        <span>another value</span>
    </div>
</div>

所以在这个例子中,我正在尝试获取h4的内容以及div类显示的每个实例的span内容。

我的相关php看起来像这样:

$doc = new DOMDocument();
@$doc->loadHTMLFile($path);

$body = $doc->getElementsByTagName('body');
$char = $body->item(0)->getElementsByTagName('div'); 

    foreach ($char as $c) {
    $test = $c->getAttribute('class');          
        if ((strpos($test,'iwantthisone') !== false) AND strpos($test,'interaction') == false)) {
            $tree = $c->getElementsByTagName('h4');
                $value = $tree->item(0)->nodeValue;        

        }
    }

我知道这段代码可以找到这个类,但是我不知道如何告诉它看它下面的树。

2 个答案:

答案 0 :(得分:0)

Xpath示例,class属性是一个令牌列表(它可以包含多个类名),因此匹配稍微复杂一些:

$html = <<<'HTML'
<div>....</div>
<div>....</div>
<div class = 'iwantthisone'>
    <h4>value</h4>
    <div class ='ilikethistoo'>
        <span>another value</span>
    </div>
</div>
HTML;

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);

$expression = '//div[
  contains(concat(" ", normalize-space(@class), " "), " iwantthisone ") or
  contains(concat(" ", normalize-space(@class), " "), " ilikethistoo ")
]';

foreach ($xpath->evaluate($expression) as $node) {
  var_dump($node->localName, $node->getAttribute('class'));
}

输出:

string(3) "div"
string(12) "iwantthisone"
string(3) "div"
string(12) "ilikethistoo"

答案 1 :(得分:0)

您可以使用正则表达式查找类名。像这样...

$doc = new DOMDocument();
@$doc->loadHTMLFile($path);

$body = $doc->getElementsByTagName('body');
$char = $body->item(0)->getElementsByTagName('div'); 

foreach ($char as $c) {
    $test = $c->getAttribute('class');          
        if (preg_match('/iwantthisone/i',$test)) {
            $tree = $c->getElementsByTagName('h4');
            $value = $tree->item(0)->nodeValue;        

        }else if(preg_match('/ilikethistoo/i',$test)){
            //do something else...
    }