2014-01-19 2 views
2
// Find all element has attribute id 
$ret = $html->find('*[id]'); 

다음은 속성 ID가있는 모든 요소를 ​​찾는 예입니다. 모든 요소를 ​​찾을 수있는 방법이 있습니까? 나는이 방법을 시도했지만 작동하지 않습니다PHP 간단한 HTML DOM Parser로 모든 요소를 ​​찾는 방법?

// Find all element 
$ret = $html->find('*'); 

추가 :

나는 $의 HTML의 모든 요소를 ​​통해 인출 할

, 모든 부모와 차일 요소를 가져온 것입니다. 예 :

<div> 
    <span> 
     <div>World!</div> 
     <div> 
      <span>Hello!</span> 
      <span> 
       <div>Hello World!</div> 
      </span> 
     </div> 
    </span> 
</div> 

지금 나는 내 자신의 일반 텍스트 모든 <span>을 탈출하고 우리가 가진 모든 <div>을 유지하려면! 예상 결과 :

<div> 
    <div>World!</div> 
    <div> 
     <div>Hello World!</div> 
    </div> 
</div> 

답변

0
/** 
* Refine the input HTML (string) and keep what was specified 
* 
* @param $string : Input HTML 
* @param array $allowed : What will be kept? 
* @return bool|simple_html_dom 
*/ 
function crl_parse_html($string, $allowed = array()) 
{ 
    // String --> DOM Elements 
    $string = str_get_html($string); 
    // Fetch child of the current element (one by one) 
    foreach ($string->find('*') as $child) { 
     if (
      // Current inner-text contain one or more elements 
      preg_match('/<[^<]+?>/is', $child->innertext) and 
      // Current element tag is in maintained elements array 
      in_array($child->tag, $allowed) 
     ) { 
      // Assign current inner-text to current filtered inner-text 
      $child->innertext = crl_parse_html($child->innertext, $allowed); 
     } else if (
      // Current inner-text contain one or more elements 
      preg_match('/<[^<]+?>/is', $child->innertext) and 
      // Current element tag is NOT in maintained elements array 
      !in_array($child->tag, $allowed) 
     ) { 
      // Assign current inner-text to the set of inner-elements (if exists) 
      $child->innertext = preg_replace('/(?<=^|>)[^><]+?(?=<|$)(<[^\/]+?>.+)/is', '$1', $child->innertext); 
      // Assign current outer-text to current filtered inner-text 
      $child->outertext = crl_parse_html($child->innertext, $allowed); 
     } else if (
      (
       // Current inner-text is only plaintext 
       preg_match('/(?<=^|>)[^><]+?(?=<|$)/is', $child->innertext) and 
       // Current element tag is NOT in maintained elements array 
       !in_array($child->tag, $allowed) 
      ) or 
      // Current plain-text is empty 
      trim($child->plaintext) == '' 
     ) { 
      // Assign current outer-text to empty string 
      $child->outertext = ''; 
     } 
    } 
    return $string; 
} 

이 내 솔루션입니다, 나는 누군가가 필요하고이 질문을 종료하면 난 그냥 여기 게시, 그것을했다.
참고 :이 함수는 재귀 적입니다. 따라서 너무 큰 데이터가 큰 문제가됩니다. 이 기능을 사용할 때는 신중하게 재검토하십시오.

1

예제가 제대로 작동하는 것으로 나타나면 다음을 시도해보십시오. 그러면 모든 요소의 innertext가 출력됩니다. 예를 들어

foreach($html->find('*') as $test) 
    echo $test->innertext; 

:

$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>'); 

출력

HelloWorld 
+0

를 찾으려면이 함수를 작성. 나는 부모로부터 자식에게 $ html의 모든 요소를 ​​가져오고 싶다. – Manhhailua

+0

그건 DOM에 액세스하는 방법이 아니에요, 내 편집을 참조하십시오. HTML과 예상 출력을 제공 할 수 있습니까? '$ html-> children()'과 같은 메소드를 사용하여 DOM 트리에 액세스해야합니다. –

+0

기본 질문에 몇 가지 세부 사항을 추가했습니다.이 부분을 살펴볼 수 있습니다 – Manhhailua

0
GLOBAL $elements; 
$elements=array(); 

findElements($fullHTML); 

function findElements($html){ 

    global $elements; 

    $art_html = new simple_html_dom(); 
    $art_html->load($html); 

    foreach ($art_html->find("*") as $element) { 

      $elements[]=$element; 
      findElements($element->innertext); 
    } 

} 

는 내가 모든 요소는 $ html로는`

Hello
World
mama
`무엇 경우

+1

단계별로 수행하는 작업을 설명합니다 미래를 도울 수있다. 회원. – Elias

관련 문제