/** HTML Attribute Filter
- * Date: 2013-09-22
- * Author: fdipzone
- * Ver: 1.0
- * Edit: bbs.it-home.org
- * Func:
- * Public strip filter attributes
- * Public setAllow: Set the allowed attributes.
- * Special case for setting public setException
- * Public setIgnore: Set the ignored flag.
- * Private findElements: Search for elements to be processed
- * Private findAttributes search attributes
- * Private removeAttributes: remove attributes
- * Private isException: determines whether a special case exists.
- * Private createAttributes: create attributes
- * Private protect special character escape
- */
-
- Class HtmlAttributeFilter {// class start
-
- Private $ _ str = ''; // source string
- Private $ _ allow = array (); // attributes that can be retained, for example, array ('id', 'class', 'title ')
- Private $ _ exception = array (); // special example: array ('a' => array ('href ', 'class '), 'span '=> array ('class '))
- Private $ _ ignore = array (); // ignore filtered tags, for example, array ('span ', 'IMG ')
-
- /** Process HTML and filter attributes that are not retained
- * @ Param String $ str Source String
- * @ Return String
- */
- Public function strip ($ str ){
- $ This-> _ str = $ str;
-
- If (is_string ($ this-> _ str) & strlen ($ this-> _ str)> 0) {// judge a string
-
- $ This-> _ str = strtolower ($ this-> _ str); // Convert it to lowercase.
-
- $ Res = $ this-> findElements ();
- If (is_string ($ res )){
- Return $ res;
- }
- $ Nodes = $ this-> findAttributes ($ res );
- $ This-> removeAttributes ($ nodes );
- }
-
- Return $ this-> _ str;
- }
-
- /** Set allowed attributes
- * @ Param Array $ param
- */
- Public function setAllow ($ param = array ()){
- $ This-> _ allow = $ param;
- }
-
- /** Set special cases
- * @ Param Array $ param
- */
- Public function setException ($ param = array ()){
- $ This-> _ exception = $ param;
- }
-
- /** Set the ignored flag
- * @ Param Array $ param
- */
- Public function setIgnore ($ param = array ()){
- $ This-> _ ignore = $ param;
- }
-
- /** Search for elements to be processed */
- Private function findElements (){
- $ 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 );
- }
- }
- }
-
- If (! $ Nodes [0]) {
- Return $ this-> _ str;
- } Else {
- Return $ nodes;
- }
- }
-
-
- /** Search for attributes
- * @ Param Array $ elements to be processed by nodes
- */
- Private function findAttributes ($ nodes ){
- 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_key];
- $ 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;
- }
-
- /** Remove attributes
- * @ Param Array $ elements to be processed by nodes
- */
- Private function removeAttributes ($ nodes ){
- Foreach ($ nodes as $ node ){
- $ Node_name = $ node ['name'];
- $ New_attributes = '';
- If (is_array ($ node ['bubuckets']) {
- Foreach ($ node ['buckets'] as $ attribute ){
- If (is_array ($ this-> _ allow) & in_array ($ attribute ['name'], $ this-> _ allow )) | $ this-> isException ($ node_name, $ attribute ['name'], $ this-> _ exception )){
- $ New_attributes = $ this-> createAttributes ($ new_attributes, $ attribute ['name'], $ attribute ['value']);
- }
- }
- }
- $ Replacement = ($ new_attributes )? "<$ Node_name $ new_attributes>": "<$ node_name> ";
- $ This-> _ str = preg_replace ('/'. $ this-> protect ($ node ['literal']). '/', $ replacement, $ this-> _ str );
- }
- }
-
- /** Determine whether a special case exists
- * @ Param String $ element_name element name
- * @ Param String $ attribute_name attribute name
- * @ Param Array $ special exceptions
- * @ Return boolean
- */
- Private function isException ($ element_name, $ attribute_name, $ exceptions ){
- If (array_key_exists ($ element_name, $ this-> _ exception )){
- If (in_array ($ attribute_name, $ this-> _ exception [$ element_name]) {
- Return true;
- }
- }
- Return false;
- }
/** Create attributes
- * @ Param String $ new_attributes
- * @ Param String $ name
- * @ Param String $ value
- * @ Return String
- */
- Private function createAttributes ($ new_attributes, $ name, $ value ){
- If ($ new_attributes ){
- $ New_attributes. = "";
- }
- $ New_attributes. = "$ name = \" $ value \"";
- Return $ new_attributes;
- }
-
- /** Escape special characters
- * @ Param String $ str Source String
- * @ Return String
- */
- Private function protect ($ str ){
- $ Conversions = array (
- "^" => "\ ^ ",
- "[" => "\[",
- "." => "\.",
- "$" => "\ $ ",
- "{" => "\{",
- "*" => "\*",
- "(" => "\(",
- "\\" => "\\\\",
- "/" => "\/",
- "+" => "\ + ",
- ")" => "\)",
- "|" => "\ | ",
- "? "=> "\? ",
- "<" => "\ <",
- ">" => "\>"
- );
- Return strtr ($ str, $ conversions );
- }
-
- } // Class end
-
- ?>
2. Demo
- Require ('htmlattributefilter. class. php ');
-
- $ Str ='
';
-
- $ Obj = new HtmlAttributeFilter ();
-
- // Allow the id attribute
- $ Obj-> setAllow (array ('id '));
-
- $ Obj-> setException (array (
- 'A' => array ('href '), // a tag allows special cases with the href attribute
- 'Ul '=> array ('class') // The ul tag allows special class attribute exceptions.
- ));
-
- // The img label is ignored and no attributes are filtered.
- $ Obj-> setIgnore (array ('IMG '));
-
- Echo 'source str:
';
- Echo htmlspecialchars ($ str ).'
';
- Echo 'filter str:
';
- Echo htmlspecialchars ($ obj-> strip ($ str ));
- ?>
Appendix: php source code used to filter html tag attribute classes |