Php filters html Tag Attribute Class usage instances and tag instances
This article describes how to filter html Tag attribute classes in php and their usage. It is a common practical skill in PHP project development. Share it with you for your reference. The specific method is as follows:
The HtmlAttributeFilter. class. php class file is as follows:
<? Php/** HTML Attribute Filter * Date: 2013-09-22 * Author: fdipzone * ver: 1.0 ** Func: * public strip filter attribute * public setAllow: allowed attribute * public setException setting special case * public setIgnore setting ignored flag * private findElements search elements to be processed * private findAttributes search attribute * private removeAttributes remove attribute * private isException determine whether special case * private createAttributes create Attribute * 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 case example: array ('A' => array ('href ', 'class '), 'span '=> array ('class') private $ _ ignore = array (); // ignore filtered tags such as 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 ($ t His-> _ str) & strlen ($ this-> _ str)> 0) {// judge the string $ this-> _ str = strtolower ($ this-> _ str); // convert it to lower case $ res = $ this-> findElements (); if (is_string ($ res) {return $ res;} $ nodes = $ this-> findAttributes ($ res); $ this-> removeAttributes ($ nodes );} return $ this-> _ str;}/** set the allowed attributes * @ param Array $ param */public function setAllow ($ param = array ()) {$ this-> _ allow = $ param;}/** set special case * @ 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 attribute * @ param Array $ elements to be processed by nodes */private function removeAttributes ($ nodes) {foreach ($ nodes as $ node) {$ node_name = $ node ['name']; $ new_attributes = ''; if (is_arr Ay ($ node ['buckets']) {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 * @ param String $ element_name element name * @ param String $ attribute_name attribute name * @ param Array $ 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 ;}/ ** Special character escape * @ param String $ str source String * @ return String */private function protect ($ str) {$ conversions = array ("^" => "\ ^", "[" => "\[",". "=> "\. "," $ "=>" \ $ "," {"=>" \ {"," * "=> "\*", "(" => "\ (", "\" => "\", "/" => "\/", "+" => "\ +", ")" => "\)", "|" => "\ | ","? "=> "\? "," <"=>" \ <","> "=>" \> "); Return strtr ($ str, $ conversions );}} // class end?>
The demo code is as follows:
<? Php require ('htmlattributefilter. class. php '); $ str = '<div class = "bd clearfix" id = "index_hilite_ul"> <ul class = "list"> <li> <div class =" cover "> <a class =" text "href =" http://www.bkjia.com "> <strong> yuna </strong> <p> love </p> </a> <strong class = "t g"> want to know </strong> <a href = "/login.html" class = "ppBtn"> <strong cl Ass = "text"> YES </strong> </a> </div> </li> </ul> </div> '; $ obj = new HtmlAttributeFilter (); // allows id attributes $ obj-> setAllow (array ('id ')); $ obj-> setException (array ('A' => array ('href '), // The a tag allows 'U' => array ('class') for special cases with the href attribute ') // ul labels can have special class attributes); // img labels are ignored, and no attributes are filtered. $ obj-> setIgnore (array ('img ')); echo 'source str: <br> '; echo htmlspecialchars ($ str ). '<br>'; echo 'filter str: <br> '; echo htmlspecialcha Rs ($ obj-> strip ($ str);?>
Click here to download the complete source code.
I hope this article will help you with PHP programming.
PHP handles HTML tags in strings
$ Html = strip_tags ($ html, ' ');
Description
String strip_tags (string str [, string allowable_tags])
This function tries to return a string with all HTML and PHP tags stripped from a given str
How can php and html tags escape from each other?
The landlord should not understand each other as nested
You need to think about something a little lower-layer.
First, php is an explanatory language and is executed by the server. That is to say, it can be understood as a "production" html
Second, html is analyzed by the browser and executed on the client.
Based on the above two points, can you draw a conclusion that php was executed before html?
Therefore, this "nested" relationship does not exist for servers.
The server only cares about the php part, and then treats html as a meaningless ghost stream.
Okay. Let's explain it in detail.
<? Php
For ($ I = 0; $ I <3; $ I ++ ){
?>
<B> NO. <? Php echo $ I?> Cycle </B>
<? Php
}
?>
Is the above paragraph a little dizzy?
In this way, remember that all the parts that php will display in html are strings printed using the print or echo method.
For example, <? Php echo $ I?>
For php, it only cares <? Php?> The for program body must start with "{" and end "}".
The last <? Php?> And the next <? Php?> The content is treated as a string by php.
So the "first loop" and <? Php echo $ I?> Php is a concept.
Of course, you can use only one <? Php?>
The preceding modification is as follows:
<? Php
For ($ I = 0; $ I <3; $ I ++ ){
Echo ("<B> TH". $ I. "th loop </B> ");
}
?>
This is an output method.
There is another
<? Php
Print <EOF
<B> loop No. {$ I} </B>
EOF;
?>
Note that you can replace "EOF" with any string, but it must end with the same string, which must be followed by ";", but before "EOF, and cannot contain any character other than the carriage return, including spaces.