PHP implementation filters various HTML tags

Source: Internet
Author: User
Tags chr html tags ord

In the process of doing the project, we often need to filter some HTML tags to improve the security of the data, in fact, is to remove those potentially harmful to the application of data. It is used to remove labels and to delete or encode unwanted characters.

First share some of the more common

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48, 49 50 51 52 53 54 55 56 57 58 59 $str =preg_replace ("/<s*imgs+[^>]*?srcs*=s* (' |") (.*?) 1[^>]*?/?s*>/i "," ", $str); Filter img Tags   $str =preg_replace ("/s+/", "", $str); Filter excess return   $STR =preg_replace ("/<[]+/si", "<", $str); Filter <__ ("<" with space behind)   $str =preg_replace ("/<!--. *?-->/si", "", $str); Comment   $str =preg_replace ("/<". *?) >/si "," ", $str); Filtration doctype   $STR =preg_replace ("/<" (/?html.*?) >/si "," ", $str); Filter HTML tags   $str =preg_replace ("/<" (/?head.*?) >/si "," ", $str); Filter head tags   $str =preg_replace ("/<" (/?meta.*?) >/si "," ", $str); Filter META tags   $str =preg_replace ("/<" (/?body.*?) >/si "," ", $str); Filter body Tags   $str =preg_replace ("/<" (/?link.*?) >/si "," ", $str); Filter link Tags   $str =preg_replace ("/<" (/?form.*?) >/si "," ", $str); Filter form Tags   $str =preg_replace ("/cookie/si", "Cookie", $str); Filter Cookie label   $STR =preg_replace ("/<" (applet.*?) > (. *?) < (/applet.*?) >/si "," ", $str); Filter Applet label   $STR =preg_replace ("/</?applEt.*?) >/si "," ", $str); Filter Applet label   $STR =preg_replace ("/<" (style.*?) > (. *?) < (/style.*?) >/si "," ", $str); Filter style label   $STR =preg_replace ("/<" (/?style.*?) >/si "," ", $str); Filter style label   $STR =preg_replace ("/<" (title.*?) > (. *?) < (/title.*?) >/si "," ", $str); Filter title tags   $str =preg_replace ("/<" (/?title.*?) >/si "," ", $str); Filter title tags   $str =preg_replace ("/<" (object.*?) > (. *?) < (/object.*?) >/si "," ", $str); Filter Object Tags   $str =preg_replace ("/<" (/?objec.*?) >/si "," ", $str); Filter Object Tags   $str =preg_replace ("/<" (noframes.*?) > (. *?) < (/noframes.*?) >/si "," ", $str); Filter noframes label   $STR =preg_replace ("/<" (/?noframes.*?) >/si "," ", $str); Filter noframes label   $STR =preg_replace ("/<" (i?frame.*?) > (. *?) < (/i?frame.*?) >/si "," ", $str); Filter Frame label   $STR =preg_replace ("/<" (/?i?frame.*?) >/si "," ", $str); Filter Frame label   $STR =preg_replace ("/<" (script.*?) > (. *?) < (/script.*?) >/si "," ", $str); //filter Script tags   $str =preg_replace ("/<" (/?script.*?) >/si "," ", $str); Filter script tags   $str =preg_replace ("/javascript/si", "JavaScript", $STR); Filter script tags   $str =preg_replace ("/vbscript/si", "VBScript", $STR); Filter script tags   $str =preg_replace ("/on ([a-z]+) S*=/si", "on1=", $str); Filter script tags   $str =preg_replace ("/&#/si", "&#", $str); Filter script Tags

A simpler formulation:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21 function delhtml ($STR) {//Clear HTML tags $st =-1;//start $et =-1;//End $stmp =array (); $stmp []= ""; $len =strlen ($STR); For ($i =0 $i < $len $i + +) {$ss =substr ($str, $i, 1); if (Ord ($SS) ==60) {//ord ("<") ==60 $st = $i;} if (Ord ($SS) ==62) {// Ord (">") ==62 $et = $i; if ($st!=-1) {$stmp []=substr ($str, $st, $et-$st + 1);}} $str =str_replace ($stmp, "", $str); return $str; }

One more:

?

1 2 3 4 5 6 function Clear_html_label ($html) {$search = Array ("' <script[^>]*?>.*?</script> ' si", "' <[/!] *? [^<>]*?> ' Si "," ' ([RN]) [s]+ ' "," ' & (quot| #34); ' I "," ' & (amp| #38); ' I "," ' & (lt| #60); ' I "," ' & (gt| #62); ' I "," ' & (nbsp| #160); ' I "," ' & (iexcl| #161); ' I "," ' & (cent| #162); ' I "," ' & (pound| #163); ' I "," ' & (copy| #169); ' I "," ' &# (d+); ' E "); $replace = Array ("", "", "1", "" "," & "," < "," > "," ", Chr (161), Chr (162), Chr (163), Chr (169)," Chr (1) "); Return Preg_replace ($search, $replace, $html); }

All of the above three methods can be achieved, but each has advantages and disadvantages, small partners according to their own project needs to choose.

Related Article

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.