PHP Regular Filter HTML

Source: Internet
Author: User

$str =preg_replace ("/\s+/", "", $str); Filter excess carriage return $str=preg_replace ("/<[]+/si", "<", $str); Filter <__ (with spaces behind "<") $str =preg_replace ("/<\!--. *?-->/si", "", $str); Note $str=preg_replace ("/< (\!. *?) >/si "," ", $str); Filter Doctype$str=preg_replace ("/< (\/?html.*?) >/si "," ", $str); Filter HTML Tags $str=preg_replace ("/< (\/?head.*?) >/si "," ", $str); Filter head tag $str=preg_replace ("/< (\/?meta.*?) >/si "," ", $str); Filter META tag $str=preg_replace ("/< (\/?body.*?) >/si "," ", $str); Filter body tag $str=preg_replace ("/< (\/?link.*?) >/si "," ", $str); Filter Link label $str=preg_replace ("/< (\/?form.*?) >/si "," ", $str); Filter Form label $str=preg_replace ("/cookie/si", "Cookie", $str); Filter cookie Tag $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 the style labelSign $str=preg_replace ("/< (title.*?) > (. *?) < (\/title.*?) >/si "," ", $str); Filter title Tag $str=preg_replace ("/< (\/?title.*?) >/si "," ", $str); Filter title Tag $str=preg_replace ("/< (object.*?) > (. *?) < (\/object.*?) >/si "," ", $str); Filter the OBJECT tag $str=preg_replace ("/<" (\/?objec.*?) >/si "," ", $str); Filter the OBJECT tag $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 the script tag $str=preg_replace ("/< (\/?script.*?) >/si "," ", $str); Filter the script tag $str=preg_replace ("/javascript/si", "JavaScript", $STR); Filter the script tag $str=preg_replace ("/vbscript/si", "VBScript", $STR); Filter the script tag $str=preg_replace ("/on ([a-z]+) \s*=/si", "on\\1=", $str); FilterScript tag $str=preg_replace ("/&#/si", "the", $str);  Filter script tags, such as Javascript:alert (

Clear spaces, line breaks

function deletehtml ($str) {$str = Trim ($str), $str = Strip_tags ($str, ""), $str = ereg_replace ("\ T", "", $str); $str = Ereg_ Replace ("\ r \ n", "", $str), $str = Ereg_replace ("\ R", "", $str), $str = ereg_replace ("\ n", "", $str); $str = Ereg_replace ("", " ", $str); return trim ($STR);}

Filtering HTML Properties

1, filter the regular expression of all HTML tags: </? [^>]+>//filters the regular expression for the properties of all HTML tags: $html = preg_replace ("/< ([a-za-z]+) [^>]*>/", "<\\1>", $html); 3, filter the partial HTML tags of the regular expression of the exclusion (such as exclude <p>, that is, do not filter <p>): </? [^pp/>]+>4, an enumeration of regular expressions that filter some HTML tags (such as filtering <a><p><b>, etc.): </? [AAPPBB] [^>]*>5, the exclusion of regular expressions that filter the properties of some HTML tags (such as excluding the ALT attribute, i.e. not filtering the ALT attribute): \s (?! ALT) [A-za-z]+=[^\s]*6, an enumeration of regular expressions that filter the properties of some HTML tags (such as the ALT attribute): (\s) alt=[^\s]*

PHP Regular Filter HTML

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.