form stripUnwantedTagsAndAttrs ( $html_str ) $xml = brand new DOMDocument (); //Suppress warnings: proper mistake approaching is out of scope of example libxml_use_internal_problems ( genuine ); //List the newest tags we should allow here, Mention you need to ensure it is html and the entire body or even entire string commonly feel removed $allowed_tags = array( “html” , “body” , Kula woman for marriage “b” , “br” , “em” , “hr” , “i” , “li” , “ol” , “p” , “s” , “span” , “table” , “tr” , “td” , “u” , “ul” ); //Checklist the newest functions we want to enable it to be here $allowed_attrs = variety ( “class” , “id” , “style” ); if (! strlen ( $html_str )) if ( $xml -> loadHTML ( $html_str , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD )) foreach ( $xml -> getElementsByTagName ( “*” ) as $mark ) when the (! in_array ( $tag -> tagName , $allowed_labels )) $level -> parentNode -> removeChild ( $level ); >otherwise foreach ( $mark -> functions as $attr ) if the (! in_number ( $attr -> nodeName , $allowed_attrs )) $tag -> removeAttribute ( $attr -> nodeName ); > > > > > return $xml -> saveHTML ();> ?>
If you want to score good tags following use remove_attrs solution, although it cannot ensure labels is actually well-balanced or included in this new appropriate framework
Just after updating out of v7.step 3.3 to v7.step 3.eight it appears nested “php tags” in to the a sequence are not any longer being stripped accurately by strip_tags().
$data = ‘Each
NewLine’ ; $new = strip_tags ( $data , ” ); var_dump ( $new ); // OUTPUTS string(11) “EachNewLine” ?>
To possess cutting-edge logic consider utilizing DOM parser
Features:* allowable labels (such as remove_tags),* elective removing options that come with the fresh new allowable tags,* recommended comment retaining,* deleting damaged and you will unclosed tags and statements,* elective callback form need each piece processed 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 ( ‘/( $comment_or_stuff ) if ( $i % 2 ) > else \”‘]++|\”[^\”]*+(?:\”|$)|'[^’]*+(?:’|$))*(?:>|$))/” , $comment_or_stuff , – 1 , PREG_SPLIT_DELIM_CAPTURE ); foreach ( _and_text as $j => $tag_or_text ) $is_broken = false ; $is_allowable = true ; $result = $tag_or_text ; if ( $j % 2 ) \”‘/]++|/+?|\”[^\”]*\”|'[^’]*’)*?(/?>)%i” , $tag_or_text , $matches ) ) $tag = strtolower ( $matches [ 2 ] ); if ( in_array ( $tag , $allowable_tags ) ) if ( $strip_attrs ) $opening = $matches [ 1 ]; $closing = ( $opening === ” : $closing ; $result = $opening . $tag . $closing ; > > else $is_allowable = false ; $result = ” ; > > else $is_broken = true ; $result = ” ; > > else if ( ! $is_broken && isset( $callback ) ) // allow result modification call_user_func_array ( $callback , array( & $result , $tag_or_text , $tag , $is_allowable ) ); > _and_text [ $j ] = $result ; > $comments_and_stuff [ $i ] = implode ( ” , _and_text ); > > $str = implode ( ” , $comments_and_stuff ); return $str ;> ?> Callback arguments:* &$result: contains text to be placed insted of original piece (e.g. empty string for forbidden tags), it can be changed;* $tag_or_text: original piece of text or a tag (see below);* $tag: false for text between tags, lowercase tag name for tags;* $is_allowable: boolean telling if a tag isn’t allowed (to avoid double checking), always true for text between tagsCallback function isn’t called for comments and broken tags.
Caution: the function does not fully validate labels (the more very HTML alone), it really force strips people definitely busted (and additionally removing taboo labels).
–>