Php 对于嵌套的DOM元素,获取某个属性的所有值(例如,所有类)

Php 对于嵌套的DOM元素,获取某个属性的所有值(例如,所有类),php,domdocument,Php,Domdocument,使用DOMDocument,如何获取domeElement嵌套组的所有类 例如,$this->xmlComponent包括: <span class="one two"><a class="three" href="#">test</a></span> 应导致[“一”、“二”、“三”]您可以使用参数*获取所有子标记,然后递归遍历所有子标记,获取每个子标记的类属性: <?php $html = '<span class="one t

使用
DOMDocument
,如何获取domeElement嵌套组的所有类

例如,
$this->xmlComponent
包括:

<span class="one two"><a class="three" href="#">test</a></span>

应导致
[“一”、“二”、“三”]

您可以使用参数
*
获取所有子标记,然后递归遍历所有子标记,获取每个子标记的
类属性:

<?php
$html = '<span class="one two"><a class="three four" href="#"><b class="five">test</b></a></span>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$spans = $dom->getElementsByTagName("span");
$values = [];
foreach ($spans as $span) {
    $values[] = $span->getAttribute("class");
    $values[] = getAllValues($span);
}

function getAllValues($node)
{
    $values = [];
    $children = $node->getElementsByTagName('*');
    foreach ($children as $child) {
        $values[] = $child->getAttribute("class");
        getAllValues($child);
    }
    return $values;
}

var_dump($values);

要使每个类都有自己的元素,只需在
中使用一个空格来分解()

foreach ($spans as $span) {
    $values[] = explode(" ", $span->getAttribute("class"));
    $values[] = getAllValues($span);
}

function getAllValues($node)
{
    $values = [];
    $children = $node->getElementsByTagName('*');
    foreach ($children as $child) {
        $values[] = explode(" ", $child->getAttribute("class"));
        getAllValues($child);
    }
    return $values;
}
结果

最后,要获取平面数组中的所有内容,请通过引用传递初始的
$values
数组:

foreach ($spans as $span) {
    $values[] = explode(" ", $span->getAttribute("class"));
    getAllValues($span, $values);
}

function getAllValues($node, &$values)
{
    $children = $node->getElementsByTagName('*');
    foreach ($children as $child) {
        $values[] = explode(" ", $child->getAttribute("class"));
        getAllValues($child, $values);
    }
    return $values;
}
结果

最后一个答案是(很抱歉写了这么长的文章),因为您提到您需要一个“特定”属性,所以您可以将此属性设置为函数,这样您就可以通过向函数(默认情况下为类)传递一个参数来获得任何属性,从而获得包含所有返回值的数组:

function getAllAttributesForNode($node, $attribute = "class")
{
    $values = [];
    foreach ($node as $child) {
        getAllValues($child, $values, $attribute);
    }

    return $values;
}

function getAllValues($node, &$values, $attribute)
{
    $values[] = explode(" ", $node->getAttribute($attribute));

    $children = $node->getElementsByTagName('*');
    foreach ($children as $child) {
        getAllValues($child, $values, $attribute);
    }

    return $values;
}

$spans = $dom->getElementsByTagName("span");
$values = getAllAttributesForNode($spans);

感谢ishegg对该方法的建议。
下面是我最终使用的压缩代码:

function getAllAttributes($nodes, $values, $attribute='class') {
    foreach ($nodes as $node) {
        $values = array_merge($values, explode(" ", $node->getAttribute($attribute)) );
        $children = $node->getElementsByTagName('*');
        $values = getAllAttributes($children, $values);
    }
    return $values; 
}

$html = '<span class="one two"><a class="three" href="#">test</a></span>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$spans = $dom->getElementsByTagName("span");

$classes = getAllAttributes($spans, []);
print_r($classes);
函数getAllAttributes($nodes,$values,$attribute='class'){
foreach($node作为$node){
$values=array_merge($values,explode(“,$node->getAttribute($attribute));
$children=$node->getElementsByTagName('*');
$values=getAllAttribute($children,$values);
}
返回$value;
}
$html='';
$dom=新的DOMDocument;
$dom->loadHTML($html);
$spans=$dom->getElementsByTagName(“span”);
$classes=getAllAttribute($span,[]);
打印(类);

感谢ishegg的思考过程和示例!
array (size=4)
  0 => 
    array (size=2)
      0 => string 'one' (length=3)
      1 => string 'two' (length=3)
  1 => 
    array (size=2)
      0 => string 'three' (length=5)
      1 => string 'four' (length=4)
  2 => 
    array (size=1)
      0 => string 'five' (length=4)
  3 => 
    array (size=1)
      0 => string 'five' (length=4)
function getAllAttributesForNode($node, $attribute = "class")
{
    $values = [];
    foreach ($node as $child) {
        getAllValues($child, $values, $attribute);
    }

    return $values;
}

function getAllValues($node, &$values, $attribute)
{
    $values[] = explode(" ", $node->getAttribute($attribute));

    $children = $node->getElementsByTagName('*');
    foreach ($children as $child) {
        getAllValues($child, $values, $attribute);
    }

    return $values;
}

$spans = $dom->getElementsByTagName("span");
$values = getAllAttributesForNode($spans);
function getAllAttributes($nodes, $values, $attribute='class') {
    foreach ($nodes as $node) {
        $values = array_merge($values, explode(" ", $node->getAttribute($attribute)) );
        $children = $node->getElementsByTagName('*');
        $values = getAllAttributes($children, $values);
    }
    return $values; 
}

$html = '<span class="one two"><a class="three" href="#">test</a></span>';
$dom = new DOMDocument;
$dom->loadHTML($html);
$spans = $dom->getElementsByTagName("span");

$classes = getAllAttributes($spans, []);
print_r($classes);