1. Remove all HTML elements, or keep a few HTML tags
Copy Code code as follows:
<?php
$text = ' <p>test paragraph.</p><!--Comment--> <a href= ' #fragment ' >other text</a> ';
echo Strip_tags ($text);
echo "/n";
Allow <p> and <a>
Echo strip_tags ($text, ' <p><a> ');
?>
The result is (minus the note):
<blockquote>test paragraph. Other text
<p>test paragraph.</p> <a href= "#fragment" >other text</a></blockquote>2. Instead, Remove only one HTML tag
Copy Code code as follows:
<?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
?>