2017-02-25 1 views
0

시간이 끝날 때까지 모든 하위 항목과 하위 항목을 반복하고 싶습니다. 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> 
+0

사용으로 볼 수있는 [RecursiveIterator] (http://docs.php.net/manual/da/class.recursiveiterator.php), 또는 Scuzzy 감사합니다 해결 재귀 함수를 만들고 내부 루프에서 참조하는 데이터를 반환하는 자체를 참조합니다. – Scuzzy

+0

그래, 그게 내가하려는 일이지만, 어떻게 시작해야할지 모르겠다. 너는 너를 도울 수 있다고 생각하니? –

+0

나는 어떤 종류의 데이터로 작업하고 결과가 어떻게 생겼는지에 대한보다 명확한 설명이 도움이 될 것이라고 생각한다. 보고있는 문제가 무엇이든 해결할 수있는 좋은 방법이 있습니다. – Scuzzy

답변

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

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