Php filters html tag attribute class usage instances, tag instances _ PHP Tutorial

Source: Internet
Author: User
Php filters html tag attribute class usage instances and tag instances. Php: examples of html tag attribute class usage. Examples of this article describe how php filters html tag attribute classes and their usage. It is a common practical skill in PHP project development. Share it with php to filter 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 ='

  • Yuna

    Love

    Want to knowYES

'; $ Obj = new HtmlAttributeFilter (); // allows the id attribute $ 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:
'; Echo htmlspecialchars ($ str ).'

'; Echo' filter str:
'; Echo htmlspecialchars ($ 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.

For ($ I = 0; $ I <3; $ I ++ ){
?>
The Cycle </B>

}
?>

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

For php, it only cares about The for program body must start with "{" and end "}".

The last And next The content is treated as a string by php.

So the "first loop" and Php is a concept.

Of course, you can use only one

The preceding modification is as follows:
For ($ I = 0; $ I <3; $ I ++ ){
Echo ("". $ I." subcycle </B> ");
}
?>

This is an output method.
There is another


Print <EOF
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.

Examples in this article describes the php html tag attribute filtering class and its usage. It is a common practical skill in PHP project development. Share with me...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.