| Php can set code for filtering and retaining related attributes. if you need code, refer.
The code is as follows:
"\ ^", "[" => "\[",". "=> "\. "," $ "=>" \ $ "," {"=>" \ {"," * "=> "\*", "(" => "\ (", "\" => "\", "/" => "\/", "+" => "\ +", ")" => "\)", "|" => "\ | ","? "=> "\? "," <"=>" \ <","> "=>" \> "); Return strtr ($ str, $ conversions );} class cleanHtml {public $ str = ''; public $ allow = array (); public $ exceptions = array (); public $ ignore = array (); public function strip ($ str) {$ this-> str = $ str; if (is_string ($ str) & strlen ($ str)> 0) {$ res = $ this-> findElements (); if (is_string ($ res) return $ res; $ nodes = $ this-> findAttributes ($ res ); $ this-> rem OveAttributes ($ nodes);} return $ this-> str;} private function findElements () {# Create an array of elements with attributes $ nodes = array (); preg_match_all ("/<([^! \/\> \ N] +) ([^>] *)>/I ", $ this-> str, $ elements ); foreach ($ elements [1] as $ el_key => $ element) {if ($ elements [2] [$ el_key]) {$ literal = $ elements [0] [$ el_key]; $ element_name = $ elements [1] [$ el_key]; $ attributes = $ elements [2] [$ el_key]; if (is_array ($ this-> ignore )&&! In_array ($ element_name, $ this-> ignore) $ nodes [] = array ('literal' => $ literal, 'name' => $ element_name, 'bubuckets' => $ attributes) ;}# Return the XML if there were no attributes to remove if (! $ Nodes [0]) return $ this-> str; else return $ nodes;} private function findAttributes ($ nodes) {# Extract attributes foreach ($ nodes as & $ node) {preg_match_all ("/([^ =] +) \ s * = \ s * [\" | '] {0, 1} ([^ \ "'] *) [\ "| '] {0, 1}/I", $ node ['bubuckets'], $ attributes); if ($ attributes [1]) {foreach ($ attributes [1] as $ att_key => $ att) {$ literal = $ attributes [0] [$ att_key]; $ attribute_name = $ attributes [1] [$ att_ke Y]; $ value = $ attributes [2] [$ att_key]; $ atts [] = array ('literal' => $ literal, 'name' => $ attribute_name, 'value' => $ value) ;}} else $ node ['bubuckets'] = null; $ node ['bubuckets'] = $ atts; unset ($ atts );} return $ nodes;} private function removeAttributes ($ nodes) {# Remove unwanted attributes foreach ($ nodes as $ node) {# Check if node has any attributes to be kept $ node_name = $ node ['na Me ']; $ new_attributes = ''; if (is_array ($ node ['bubuckets']) {foreach ($ node ['bubuckets'] as $ attribute) {if (is_array ($ this-> allow) & in_array ($ attribute ['name'], $ this-> allow )) | $ this-> isException ($ node_name, $ attribute ['name'], $ this-> exceptions) $ new_attributes = $ this-> createAttributes ($ new_attributes, $ attribute ['name'], $ attribute ['value']); }}$ replacement = ($ ne W_attributes )? "<$ Node_name $ new_attributes>": "<$ node_name>"; $ this-> str = preg_replace ('/'. reg_escape ($ node ['literal']). '/', $ replacement, $ this-> str) ;}} private function isException ($ element_name, $ attribute_name, $ exceptions) {if (array_key_exists ($ element_name, $ this-> exceptions) {if (in_array ($ attribute_name, $ this-> exceptions [$ element_name]) return true;} return false;} private functi On createAttributes ($ new_attributes, $ name, $ value) {if ($ new_attributes) $ new_attributes. = ""; $ new_attributes. = "$ name = \" $ value \ ""; return $ new_attributes ;}}?>2. call example:
Allow = array ('id'); $ sa-> exceptions = array ('IMG '=> array ('src', 'alt '), 'A' => array ('href ', 'title'), 'iframe' => array ('src', 'frameborder '),); echo $ str = $ sa-> strip ($ str);?> |