开发者

PHP: How to strip tags in a string with certain attributes that have certain values?

I need a function like this:

function strip_tags_with_attribute_values($string, $allowedTags, $allowedAttribute, $allowedValue) {

   ...

}

And it must produce results like this:

$str = '<p class="bla">hello1</p><p class='b开发者_JS百科la2'>hello2</p>';
echo strip_tags_with_attribute_values($str, '<p>', 'class', 'bla');

Must produce:

hello1<p class='bla2'>hello2</p>

Why do I need this? Users copy and paste text from word into the FCKEditor (in Drupal). I need to strip out all the style attributes from the p and span tags.


In your case, using something as simple as

$str = preg_replace("/<p class=\"(bla)\">(.+?)<\/p>/is", "$2", $str);

Should work. If you want arguments, you could try

function strip_tags_with_attribute_values($str, $tag, $att, $val)
{
    $pat = "/<{$tag} {$att}=\"{$val}\">(.+?)<\/{$tag}>/is";
    $str = preg_replace($pat, "$1", $str);
    return $str;
}

Or something similar. This wont work correctly if a tag has multiple attributes. If that's your case, you'll probably want to try using a DOM object or XPATH to strip those out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜