2

特定のhtmlタグを削除して、それらの一部を許可するにはどうすればよいですか?

例えば、

spanタグを削除したいのですがspan、下線付きを許可します。

<span style="text-decoration: underline;">Text</span>

許可したいのですが、たとえば、p内部のスタイルやクラスを削除したいのですが、p

<p class="99light">Text</p>pタグ内のクラスを削除する必要があります-クリーンなpタグが必要です。

これは私がこれまでに持っているラインです、

strip_tags($content, '<p><a><br><em><strong><ul><li>');
4

2 に答える 2

1

できません。これを行うには、XML/HTML パーサーを使用する必要があります。

// with DOMDocument it might look something like this.
$dom = new DOMDocument();
$dom->loadHTML( $content );
foreach( $dom->getElementsByTagName( "p" ) as $p )
{
    // removes all attributes from a p tag.
    /*
    foreach( $p->attributes as $attrib )
    {
        $p->removeAttributeNode( $attrib );
    }
    */
    // remove only the style attribute.
    $p->removeAttributeNode( $p->getAttributeNode( "style" ) );
}
echo $dom->saveHTML();
于 2011-07-22T15:24:13.737 に答える
0

You need full DOM parsing. strip_tags will not offer the necessary security and customization. I have used the HTMLPurifier library in the past for this. It does actual parsing and allows you to set whitelists while taking care of malicious inputs and producing valid markup!

By "necessary security" I mean that if you try to write a custom parser you will make a mistake (don't worry, I would too) and by "customization" I mean no built-in solution will let you target only certain tags with certain attributes and values of those attributes. HTMLPurifier is the PHP library solution.

于 2011-07-22T15:22:20.300 に答える