0

助けが必要です。私は正規表現を調べましたが、その実装をまだ完全には理解していません。親に指定されたクラスまたはIDが含まれている場合、すべてのタグとその子を削除するスニペットが必要です。

例:

<?php

function remove_tag($find="",$html)
{
    # Remove multiple #IDs and classes at once

    # When given a string (separating objects with a comma)
    if (is_string($find))
    {
        $objects = explode(',', str_replace(' ', '', $find);
    } else if (is_array($find)) {
        $objects = $find;
    }

    foreach ($objects as $object)
    {
        # If ID
        if (substr($object,0,1) == '#')
        {
            # regex to remove an id
            # Ex: '<ANYTAG [any number of attributes] id='/"[any number of ids] NEEDLE [any number of ids]'/" [any number of attributes]>[anything]</ENDTAG [anything]>'

        }

        if (substr($object,0,1) == '.')
        {
            # remove a class
            # Ex: '<ANYTAG [any number of attributes] class='/"[any number of classes] NEEDLE [any number of classes]'/" [any number of attributes]>[anything]</ENDTAG [anything]>'
        }

        # somehow remove it from the $html variable?
    }
}

これが初心者の質問である場合は申し訳ありませんが、お時間をいただきありがとうございます!:)

-パット

4

1 に答える 1

2

正規表現の代わりにXPathを使用して、削除するドキュメント内のすべての要素を検索できます。

DOMDocumentXPathは、私にとって良いスタートのように思えます。

DOMNode::removeChild()メソッドを使用して子を削除し、DOMXPathクラスを使用してXPathを評価し、削除する必要のあるノードを取得できます。

于 2013-02-16T00:24:03.923 に答える