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 に答える