如何使用PHP Simple HTML DOM Parser查找所有元素?

时间:2014-01-19 18:09:47

标签: php html web-crawler simpledom

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

这是查找具有属性id的所有元素的示例。有没有办法找到所有元素。我尝试这种方式,但它不起作用:

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

附加:

我想获取$ html中的所有元素,将获取所有parent和childs元素。例如:

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
}

这是我的解决方案,我做到了,如果有人需要它,我只是在这里发帖并结束这个问题 请注意: 此函数使用递归。因此,太大的数据将是一个大问题。决定使用此功能时要仔细重新考虑。

答案 2 :(得分: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);
     }

}

我写这个函数来找到所有元素