setting stripUnwantedTagsAndAttrs ( $html_str ) $xml = the fresh DOMDocument (); //Prevents cautions: correct mistake dealing with may be out of scope from analogy libxml_use_internal_errors ( true ); //Number new tags we need to enable it to be here, Notice you must enable it to be html and the entire body otherwise whole string will become eliminated $allowed_labels = array( “html” , “body” , “b” , “br” , “em” , “hr” , “i” , “li” , “ol” , “p” , “s” , “span” , “table” , “tr” , “td” , “u” , “ul” ); //Checklist brand new services we should allow right here $allowed_attrs = number ( “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 ( “*” ) once the $tag ) in the event the (! in_variety ( $level -> tagName , $allowed_labels )) $level -> parentNode -> removeChild ( $level ); >more foreach ( $level -> functions because the $attr ) in the event the (! in_variety ( $attr -> nodeName , $allowed_attrs )) $tag -> removeAttribute ( $attr -> nodeName ); > > > > > get back $xml -> saveHTML ();> ?>
If you want to rating valid labels next play with remove_attrs solution, although it will not ensure labels try well-balanced or included in the latest suitable framework
After upgrading off v7.3.3 to v7.step three.seven it appears nested “php tags” to the a set are no offered getting removed accurately by remove_tags().
$data = ‘Each
NewLine’ ; $new = strip_tags ( $data , ” ); var_dump ( $new ); // OUTPUTS string(11) “EachNewLine” ?>
To have cutting-edge reasoning consider utilizing DOM parser
Features:* deductible tags (as with remove_tags),* optional removing attributes of the newest allowable labels,* optional remark sustaining,* deleting damaged and you can unclosed tags and you will comments,* optional callback function require each piece canned allowing for flexible replacements.
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 ( ‘/(