function stripUnwantedTagsAndAttrs ( $html_str ) $xml = the DOMDocument (); //Inhibits cautions: proper error handling is beyond scope regarding example libxml_use_internal_errors ( https://gorgeousbrides.net/no/costa-rican-bruder/ true ); //List the brand new tags we want to allow it to be here, Mention you should make it html and body if not entire sequence commonly getting eliminated $allowed_tags = array( „html“ , „body“ , „b“ , „br“ , „em“ , „hr“ , „i“ , „li“ , „ol“ , „p“ , „s“ , „span“ , „table“ , „tr“ , „td“ , „u“ , „ul“ ); //Checklist new features we should succeed right here $allowed_attrs = array ( „class“ , „id“ , „style“ ); if the (! strlen ( $html_str )) if the ( $xml -> loadHTML ( $html_str , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD )) foreach ( $xml -> getElementsByTagName ( „*“ ) given that $mark ) when the (! in_range ( $tag -> tagName , $allowed_labels )) $tag -> parentNode -> removeChild ( $level ); >more foreach ( $mark -> qualities because $attr ) if the (! in_selection ( $attr -> nodeName , $allowed_attrs )) $level -> removeAttribute ( $attr -> nodeName ); > > > > > return $xml -> saveHTML ();> ?>
If you want to get good tags upcoming explore remove_attrs option, though it does not ensure tags are healthy or used in brand new compatible framework
Shortly after upgrading out of v7.step three.step 3 to v7.3.7 it appears nested „php labels“ in to the a sequence are not any prolonged being removed accurately because of the remove_tags().
$data = ‚Each
NewLine‘ ; $new = strip_tags ( $data , “ ); var_dump ( $new ); // OUTPUTS string(11) „EachNewLine“ ?>
To possess advanced reasoning consider utilizing DOM parser
Features:* allowable labels (such as strip_tags),* optional stripping top features of new deductible labels,* elective comment sustaining,* deleting broken and unclosed labels and you may statements,* optional callback mode need each piece canned allowing for flexible alternatives.
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 ( ‚/(