1. Remove all html elements, or keep certain html tags
Copy the code The code is as follows:
$text = '
Test paragraph.
Other text ';
echo strip_tags($text);
echo "/n";
// Allow
and
echo strip_tags($text, '');
?>
The result is (with comments removed):
Test paragraph. Other text
Test paragraph.
Other text
2 .On the contrary, only remove a certain html tag
Copy code The code is as follows:
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)
[^>]*>|)';
$str = preg_replace('#?'.$tag.'[^>]*>'.$content.'#is', '', $str);
}
return $str;
}
$str = 'red text';
$tags = 'font';
$a = strip_only($str, $tags); // red text
$b = strip_only($str, $tags, true); // text
?>
http://www.bkjia.com/PHPjc/621724.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621724.htmlTechArticle1. Remove all html elements, or keep certain html tags. Copy the code as follows: ?php $text = 'pTest paragraph./p!-- Comment -- a href="#fragment"Other text/a'; echo strip_...