How to strip HTML tags using a black list in PHP?

那年仲夏 提交于 2019-12-02 16:44:55

问题


PHP strip_tags use a whitelist for skip some tags that you don't want were get rid. Anybody knows some implementation but using a blacklist instead of a whitelist?


回答1:


A simple compound regex search would work (if this is still about your previous issue):

$html =
preg_replace("#</?(font|strike|marquee|blink|del)[^>]*>#i", "", $html);



回答2:


Try this function posted by LWC on php.net - http://www.php.net/manual/en/function.strip-tags.php#96483

<?php
function strip_only($str, $tags, $stripContent = false) {
    $content = '';
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) {
        if ($stripContent)
             $content = '(.+</'.$tag.'[^>]*>|)';
         $str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $str);
    }
    return $str;
}

$str = '<font color="red">red</font> text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?> 


来源:https://stackoverflow.com/questions/4996977/how-to-strip-html-tags-using-a-black-list-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!