Note that allowing point tags otherwise href attributes opens up other prospective protection gap that the solution would not prevent

Note that allowing point tags otherwise href attributes opens up other prospective protection gap that the solution would not prevent

setting stripUnwantedTagsAndAttrs ( $html_str ) $xml = brand new DOMDocument (); //Inhibits warnings: best mistake dealing with is beyond range off example libxml_use_internal_problems ( true ); //Record the new labels you want to enable it to be here, Notice you must succeed html and the entire body otherwise whole sequence have a tendency to end up being cleaned $allowed_tags = array( “html” , “body” , “b” , “br” , “em” , “hr” , “i” , “li” , “ol” , “p” , “s” , “span” , “table” , “tr” , “td” , “u” , “ul” ); //Listing new services you want to allow here $allowed_attrs = range ( “class” , “id” , “style” ); when the (! strlen ( $html_str )) in the event that ( $xml -> loadHTML ( $html_str , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD )) foreach ( $xml -> getElementsByTagName ( “*” ) while the $tag ) if (! in_assortment ( $tag -> tagName , $allowed_labels )) $level -> parentNode -> removeChild ( $level ); >more foreach ( $tag -> services since $attr ) in the event the (! in_assortment ( $attr -> nodeName , $allowed_attrs )) $level -> removeAttribute ( $attr -> nodeName ); > > > > > return $xml -> saveHTML ();> ?>

Should you want to score legitimate tags following have fun with strip_attrs solution, though it doesn’t ensure tags was balanced otherwise found in new suitable context

Shortly after updating off v7.step three.3 to help you v7.3.seven it appears nested “php labels” in to the a series are no lengthened are removed accurately by remove_tags().

$data = ‘Each
NewLine’ ; $new = strip_tags ( $data , ” ); most attractive guams girl var_dump ( $new ); // OUTPUTS string(11) “EachNewLine” ?>

To own cutting-edge reasoning contemplate using DOM parser

Features:* allowable tags (as with strip_tags),* optional stripping attributes of this new allowable labels,* optional remark sustaining,* deleting damaged and you will unclosed tags and you may statements,* elective callback function call for each piece processed allowing for versatile 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 ( ‘/( $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: case cannot fully verify tags (the greater therefore HTML itself), it just push strips people however damaged (as well as removing taboo tags).

-->

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *