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

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

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

3 回答 3

1

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

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

例如:

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

输出

HelloWorld
于 2014-01-19T18:16:48.033 回答
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);
     }

}

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

于 2014-09-26T16:13:06.177 回答
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;
}

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

于 2014-02-05T12:46:16.897 回答