Remember that enabling point tags otherwise href functions reveals various other possible security hole that solution would not prevent

form stripUnwantedTagsAndAttrs ( $html_str ) $xml = the DOMDocument (); //Suppress cautions: right error handling may be out of extent out-of example libxml_use_internal_errors ( real ); //List the fresh labels you want to ensure it is right here, Note you ought to enable it to be html and the body if not entire string have a tendency to getting eliminated $allowed_tags = array( “html” , “body” , “b” , “br” , “em” , “hr” , “i” , “li” , “ol” , “p” , “s” , “span” , “table” , “tr” , “td” , “u” , “ul” ); //Checklist the latest properties we should allow here $allowed_attrs = range ( “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 ( “*” ) as $level ) if (! in_selection ( $tag -> tagName , $allowed_tags )) $level -> parentNode -> removeChild ( $mark ); >more foreach ( $mark -> attributes just like the $attr ) in the event that (! in_selection ( $attr -> nodeName , $allowed_attrs )) $mark -> removeAttribute ( $attr -> nodeName ); > > > > > get back $xml -> saveHTML ();> ?>

If you would like rating valid tags next fool around with remove_attrs solution, although it doesn’t make sure tags was healthy or utilized in the suitable context

Immediately after updating of v7.3.step 3 in order to v7.step 3.eight it appears nested “php labels” in to the a sequence are not any longer getting stripped truthfully of the strip_tags().

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

To have advanced logic contemplate using DOM parser

Features:* deductible tags (like in strip_tags),* optional stripping features of the brand new allowable labels,* recommended feedback preserving,* removing busted and you can unclosed tags and you can comments,* elective callback mode require every piece canned making it possible 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 ( ‘/(

Write a Comment

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

0 / 10