PHP Filter HTML markup Attribute class usage instance, tag instance _php tutorial

Source: Internet
Author: User
Tags php programming

PHP Filter HTML tag attribute class Usage instance, tag instance


This article describes the PHP filter HTML tag attribute class and its usage. is a common practical skill in the development of PHP projects. Share to everyone for your reference. Here's how:

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 Properties * Public Setallow set allowed properties * Public setexception Set Exceptions * Public Setignore set ignored tags * private findelement s search for elements to be processed * Private findattributes Search Property * Private Removeattributes Remove attribute * Private isexception to determine if special case * PRIV  Ate createattributes Create attribute * Private protect special Character escapes */class htmlattributefilter{//class start Private $_str =      '';   SOURCE string Private $_allow = Array (); Allow reserved properties such as: Array (' ID ', ' class ', ' title ') Private $_exception = Array ();  Exceptions For example: Array (' A ' =>array (' href ', ' class '), ' span ' =>array (' class ')) Private $_ignore = Array (); Ignore filtered tags such as: Array (' span ', ' img ')/** processing HTML, filtering properties not reserved * @param string $str Source String * @return String * *      Ction Strip ($str) {$this->_str = $str; if (is_string ($this->_str) && strlen ($this->_str) >0) {//judging string $this->_str = StrtoloweR ($this->_str);       Turn into lowercase $res = $this->findelements ();       if (is_string ($res)) {return $res;       } $nodes = $this->findattributes ($res);     $this->removeattributes ($nodes);   } return $this->_str;   }/** set the allowed properties * @param Array $param */Public Function Setallow ($param =array ()) {$this->_allow = $param; }/** Set Exception * @param Array $param */Public Function setexception ($param =array ()) {$this->_exception = $pa   Ram    }/** set the ignored tag * @param Array $param */Public Function Setignore ($param =array ()) {$this->_ignore = $param;     /** search for the element 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 (' Litera         L ' = $literal, ' name ' = $element _name, ' attributes ' + $attributes);     }}} if (! $nodes [0]) {return $this->_str;     }else{return $nodes; }}/** Search properties * @param Array $nodes The element to be processed */Private function findattributes ($nodes) {foreach ($nodes as &A MP; $node) {Preg_match_all ("/([^ =]+) \s*=\s*[\" |] {0,1} ([^\"']*) [\"|']       {0,1}/i ", $node [' Attributes '], $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 [' attributes '] = NULL;       } $node [' attributes '] = $atts;Unset ($atts);   } return $nodes; /** Remove attribute * @param Array $nodes The element to be processed */Private function removeattributes ($nodes) {foreach ($nodes as $nod       e) {$node _name = $node [' name '];       $new _attributes = "; if (Is_array ($node [' attributes '])) {foreach ($node [' attributes '] as $attribute) {if (Is_array ($this->_a llow) && In_array ($attribute [' name '], $this->_allow)) | | $this->isexception ($node _name, $attribute [' name '], $this->_exception)) {$new _attributes = $this->crea           Teattributes ($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); }}/** to determine if the exception is * @param string $element _name element name * @param string $attribute _name property name * @param Array $exception  s allowed exceptions * @return Boolean */Private Function isexception ($element _name, $attribute _name, $exceptions) {if (Array_key_exis         TS ($element _name, $this->_exception)) {if (In_array ($attribute _name, $this->_exception[$element _name])) {       return true;   }} return false;   /** Create Property * @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 escapes * @param string $str Source String * @return String */Private Function Protect ($STR) {$conversions =        Array ("^" = "\^", "[" = "\[", "." = "\.", "$" = "\$", "{" = "=", "\{",        "*" = "\*", "(" "=" \ "," \ \ "=" \\\\ ","/"="/"," + "=" \+ ", ")" = "\)",        "|" = "\|", "?" = "\", "<" and "\<", ">" and "\>");   Return Strtr ($str, $conversions); }}//class end?>

The demo sample code is as follows:

<?php require (' HtmlAttributeFilter.class.php ');  $str = '
 
  
  
  • Yuna

    Love

    want to know YES
'; $obj = new Htmlattributefilter (); The Allow ID attribute $obj->setallow (array (' ID ')); $obj->setexception (' a ' = ' = ' Array (' href '), //A tag allows a special case of href attribute ' ul ' = = = Array (' class ')// UL tags allow class attribute exceptions); The IMG tag ignores, does not filter any attributes $obj->setignore (Array (' IMG ')); echo ' Source str:
'; echo Htmlspecialchars ($str). '

'; Echo ' Filter str:

This article full source click here to download this machine.

I hope this article is helpful to everyone's PHP programming.


PHP Handling HTML tags in string issues

$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 does PHP's markup and HTML tags escape each other?

Landlord please do not understand into the mutual nesting

You need to think about something a little bit lower.

First, PHP is an explanatory language, executed by the server, that is, it can be understood that it is used to "produce" html
Second, HTML is parsed by the browser and is performed at the client

According to the above two points, can you conclude that PHP was executed before HTML?

Therefore, this "nested" relationship, for the server, there is no.
The server only cares about the parts of PHP, and then treats HTML as meaningless stream of characters.

All right, here's the exact explanation.

for ($i =0; $i <3; $i + +) {
?>
the first cycle </b>

}
?>

Is this part of the above looking a little dizzy?

In this regard, remember that the part that PHP displays in HTML is all the strings printed with the print or Echo method.
Such as

For PHP, it will only care about the program body, the for program body, must start with "{" To End with "}"

The content between the previous and the next is treated by PHP as a string.

So the "first cycle" and for PHP is a concept.

Of course, you can use only one

The above changes are as follows:
for ($i =0; $i <3; $i + +) {
Echo (" No.". $i. " Cycles </b> ");
}
?>

This is an output mode
There's another one.


Print <<< EOF
{$i} cycles </b>
EOF;
?>

This way note that "EOF" you can replace with any string, but must end with the same string, after which the string must have ";", but, "EOF;" Before, and after, there cannot be any characters outside of the carriage, including spaces.

http://www.bkjia.com/PHPjc/882891.html www.bkjia.com true http://www.bkjia.com/PHPjc/882891.html techarticle PHP Filter HTML tag attribute class Usage instance, tag instance This article describes the PHP filter HTML tag attribute class and its usage. is a common practical skill in the development of PHP projects. Share to the big ...

  • 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.