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

这是查找所有具有属性ID的元素的示例。有什么方法可以找到所有元素。我尝试这种方式,但它不工作:如何使用PHP Simple DOM DOM分析器查找所有元素?

// 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

您的示例似乎正常工作,请尝试以下操作,它将输出每个元素的内联网文本。

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是'

Hello
World
mama
'。我的意思是我想通过$ html的所有元素,从父母到孩子。 – Manhhailua

+0

这不是如何访问DOM的作品,请参阅我的编辑。你能提供一些HTML和你的预期输出。您需要使用'$ html-> children()' –

+0

等方法访问DOM树。我已经为主要问题添加了一些细节,您可以查看它 – 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); 
    } 

} 

我写此功能找到的所有元素

+1

解释您的函数逐步执行的操作可以帮助未来SO成员。 – Elias