function stripUnwantedTagsAndAttrs ( $html_str ) $xml = this new DOMDocument (); //Suppresses cautions: best error addressing is out of extent regarding example libxml_use_internal_errors ( real ); //Number the new tags we need to allow it to be here, Note you ought to create html and the entire body or even entire string commonly feel removed $allowed_tags = array( “html” , “body” , “b” , “br” , “em” , “hr” , “i” , “li” , “ol” , “p” , “s” , “span” , “table” , “tr” , “td” , “u” , “ul” ); //Checklist the fresh new features we want to allow right here $allowed_attrs = selection ( “class” , “id” , “style” ); in the event the (! strlen ( $html_str )) if the ( $xml -> loadHTML ( $html_str , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD )) foreach ( $xml -> getElementsByTagName ( “*” ) since $tag ) in the event the (! in_selection ( $level -> tagName , $allowed_labels )) $tag -> parentNode -> removeChild ( $level ); >more foreach ( $mark -> features as $attr ) if the (! in_variety ( $attr -> nodeName , $allowed_attrs )) $level -> removeAttribute ( $attr -> nodeName ); > > > > > return $xml -> saveHTML ();> ?>
If you would like get legitimate tags up coming have fun with strip_attrs choice, although it does not be sure tags is actually well-balanced or found in the fresh new compatible perspective
Shortly after upgrading out of v7.3.3 so you can v7.3.7 it appears nested “php tags” in to the a sequence are not any prolonged are stripped accurately from the remove_tags().
$data = ‘Each
NewLine’ ; $new = strip_tags ( $data , ” ); var_dump ( $new ); // OUTPUTS string(11) “EachNewLine” ?>
To own advanced reasoning contemplate using DOM parser
Features:* deductible labels (as with strip_tags),* elective removing features of the latest deductible labels,* optional comment sustaining,* deleting damaged and you may unclosed tags and comments,* elective callback function call for every piece processed making it possible for flexible 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 ( ‘/(