如何做到这一点循环通过所有的孩子和子孩子,直到没有更多的孩子?

时间:2017-02-25 06:58:11

标签: php simple-html-dom

我想循环通过所有的孩子和子孩子直到时间结束。它从HTML STRING开始,有些元素包含多达7或8层子节点。 如何以聪明的方式做到这一点?

<code>
include_once('simple_html_dom.php');
$style_array = array();
foreach(str_get_html($str)->find('*') as $element) {
$PARENT_NODE = new stdClass();
$PARENT_NODE->tag = $element->tag;
$PARENT_NODE->style = $element->style;
$PARENT_NODE->src = $element->src;
$PARENT_NODE->href = $element->href;
$PARENT_NODE->innertext = array();
if($element->hasChildNodes()) {
foreach(str_get_html($element->innertext)->find('*') as $element2) {
$CHILD_NODE_1 = new stdClass();
$CHILD_NODE_1->tag = $element2->tag;
$CHILD_NODE_1->style = $element2->style;
$CHILD_NODE_1->src = $element2->src;
$CHILD_NODE_1->href = $element2->href;
$CHILD_NODE_1->innertext = array();
if($element2->hasChildNodes()) {
foreach(str_get_html($element->innertext)->find('*') as $element3) {
$CHILD_NODE_2 = new stdClass();
$CHILD_NODE_2->tag = $element3->tag;
$CHILD_NODE_2->style = $element3->style;
$CHILD_NODE_2->src = $element3->src;
$CHILD_NODE_2->href = $element3->href;
$CHILD_NODE_2->innertext = $element3->innertext;
array_push($CHILD_NODE_1->innertext, $CHILD_NODE_2);
}
}else{
$CHILD_NODE_1->innertext = $element2->innertext;
}
array_push($PARENT_NODE->innertext, $CHILD_NODE_1);
}
}else{
$PARENT_NODE->innertext = $element->innertext;
}
array_push($style_array,array($PARENT_NODE));
};
echo var_export($style_array, true);  
</code>

2 个答案:

答案 0 :(得分:1)

解决它,谢谢你Scuzzy

$html = str_get_html($str);
$output = process_html( $html );
echo '<pre>' . var_export($output, true) . '</pre>';
function process_html( $nodes )
{
  $array = array();
  foreach(str_get_html($nodes)->find('*') as $node) {
    $object = new stdClass();
    $object->tag = $node->tag;
    $object->href = $node->href;
    $object->src = $node->src;
    $object->style = $node->style;
    $object->innertext = $node->innertext;
    if( $node->hasChildNodes())
    {
      $object->innertext = process_html( $node->innertext );
    }
    $array[] = $object;
  }
  return $array;
}

答案 1 :(得分:0)

这是我能帮到你的最好的,展示了如何递归simplexml来构建一个标准的数组/对象结构。

$html = simplexml_load_string( file_get_contents('https://en.wikipedia.org/wiki/HTML5') );

$output = process_html( $html );

print_r($output);

function process_html( SimpleXMLElement $nodes )
{
  $array = array();
  /* @var $node SimpleXMLElement */
  foreach( $nodes as $node )
  {
    $object = new stdClass();
    $object->tag = $node->getName();
    $object->text = trim( (string) $node );
    if( $node->attributes() )
    {
      $object->attributes = new stdClass();
      foreach( $node->attributes() as $attrKey => $attr )
      {
        $object->attributes->{$attrKey} = (string) $attr;
      }
    }
    if( count( $node->children() ) )
    {
      // Here is the recursion
      $object->children = process_html( $node->children() );
    }
    $array[] = $object;
  }
  return $array;
}