mode stripUnwantedTagsAndAttrs ( $html_str ) $xml = this new DOMDocument (); //Prevents cautions: proper error approaching may be out of extent off example libxml_use_internal_errors ( real ); //List the latest tags we need to allow it to be right here, Notice you ought to enable it to be html and the entire body if not whole sequence will end up being cleared $allowed_tags = array( «html» , «body» , «b» , «br» , «em» , «hr» , «i» , «li» , «ol» , «p» , «s» , «span» , «table» , «tr» , «td» , «u» , «ul» ); //List the brand new properties we would like to make it here $allowed_attrs = number ( «class» , «id» , «style» ); if (! strlen ( $html_str )) if the ( $xml -> loadHTML ( $html_str , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD )) foreach ( $xml -> getElementsByTagName ( «*» ) just like the $level ) if the (! in_number ( $mark -> tagName , $allowed_tags )) $mark -> parentNode -> removeChild ( $mark ); >else foreach ( $tag -> characteristics because the $attr ) if (! in_array ( $attr -> nodeName , $allowed_attrs )) $tag -> removeAttribute ( $attr -> nodeName ); > > > > > go back $xml -> saveHTML ();> ?>
Should you want to get legitimate labels upcoming play with remove_attrs alternative, although it will not guarantee tags are balanced or found in brand new compatible perspective
Shortly after upgrading regarding v7.3.step 3 so you’re able to v7.step three.eight it seems nested «php labels» into the a set are not any lengthened being stripped precisely because of the remove_tags().
$data = ‘Each
NewLine’ ; $new = strip_tags ( $data , » ); var_dump ( $new ); // OUTPUTS string(11) «EachNewLine» ?>
To possess cutting-edge reason contemplate using DOM parser
Features:* deductible labels (as with remove_tags),* elective removing options that come with new deductible labels,* recommended review preserving,* removing broken and you will unclosed labels and you can statements,* recommended callback form need each piece canned making it possible for versatile substitutes.
function better_strip_tags ( $str , $allowable_tags = » , $strip_attrs = false , $preserve_comments = false , callable $callback = null ) $allowable_tags = array_map ( ‘strtolower’ , array_filter ( // lowercase preg_split ( ‘/(?:>|^)\\s*(?: <|$)/'>// filter broken ) ); $comments_and_stuff = preg_split ( ‘/(