PHP removes unnecessary HTML, HTML crit, and Css labels.

Source: Internet
Author: User
This article will introduce various PHP methods and implementation programs for removing redundant HTML, Javascrit, and Css labels. 1. without retaining any HTML tags, the code will be like this: echostrip_tags ($ s... this article will introduce various PHP methods and implementation programs for removing redundant HTML, Javascrit, and Css labels.

1. do not retain any HTML tags. the code will be like this: echo strip_tags ($ str );

2. retain only

For a tag, you only need

The string is written to the second parameter of strip_tags. the code will be like this: echo strip_tags ($ str ,"");

3. we want to retain

And... Multiple tags, separated by spaces, must be written to the second parameter of strip_tags. the code will be like this: echo strip_tags ($ str ,"");

4. keep all tags and only escape functions such as addslashes (), stripslashes (), htmlspecialchars (), htmlentities (), and nl2br.

Addslashes () and stripslashes () are generally used when the database is in and out of the warehouse, so as not to store keywords such as quotation marks in the variables. in this case, if a part of the content is recognized as an identifier by the database for execution, an error will occur.

The htmlspecialchars () function is only used to escape a small amount of HTML, &, double quotation marks, greater than or less than signs. it is not converted to the ASCII conversion specified in HTML.

Htmlentities () this function is a bit like the htmlspecialchars () function, but this function converts all string characters into special character set strings of HTML. however, there will be a lot of troubles in reading the source code of the webpage after conversion, especially when the text in the source code of the webpage becomes invisible to the cloud, and what the browser sees is still normal.

Built-in functions remove html tags

Strip_tags

Remove HTML and PHP tags.

Syntax: string strip_tags (string str );

Return value: string

Correspondence type: data processing

The instance code is as follows:

 

Convert Special characters into HTML string format (...). The most common application may be to process the message board of the customer's message.

& (And) &

"(Double quotation marks)"

<(Less than) to <

> (Greater than) to>

This function only converts the preceding special characters and does not convert all of them to the ASCII conversion specified in HTML.

Here, only html, js, and css are replaced.

The instance code is as follows:

 /Si "," ", $ descclear); // filter html comments $ descclear = preg_replace ("/<(\!. *?)> /Si "," ", $ descclear); // filter DOCTYPE $ descclear = preg_replace ("/<(\/? Html. *?)> /Si "," ", $ descclear); // filter html tags $ descclear = preg_replace ("/<(\/? Head. *?)> /Si "," ", $ descclear); // filter head labels $ descclear = preg_replace ("/<(\/? Meta. *?)> /Si "," ", $ descclear); // filter meta Tags $ descclear = preg_replace ("/<(\/? Body. *?)> /Si "," ", $ descclear); // filter the body tag $ descclear = preg_replace ("/<(\/? Link. *?)> /Si "," ", $ descclear); // filter link tags $ descclear = preg_replace ("/<(\/? Form. *?)> /Si "," ", $ descclear); // filter form labels $ descclear = preg_replace ("/cookie/si "," COOKIE ", $ descclear ); // filter the COOKIE tag $ descclear = preg_replace ("/<(applet. *?)> (.*?) <(\/Applet. *?)> /Si "," ", $ descclear); // filter the applet tag $ descclear = preg_replace ("/<(\/? Applet. *?)> /Si "," ", $ descclear); // filter the applet tag $ descclear = preg_replace ("/<(style. *?)> (.*?) <(\/Style. *?)> /Si "," ", $ descclear); // filter the style label $ descclear = preg_replace ("/<(\/? Style. *?)> /Si "," ", $ descclear); // filter the style label $ descclear = preg_replace ("/<(title. *?)> (.*?) <(\/Title. *?)> /Si "," ", $ descclear); // filter the title tag $ descclear = preg_replace ("/<(\/? Title. *?)> /Si "," ", $ descclear); // filter the title tag $ descclear = preg_replace ("/<(object. *?)> (.*?) <(\/Object. *?)> /Si "," ", $ descclear); // filter the object tag $ descclear = preg_replace ("/<(\/? Objec. *?)> /Si "," ", $ descclear); // filter the object tag $ descclear = preg_replace ("/<(noframes. *?)> (.*?) <(\/Noframes. *?)> /Si "," ", $ descclear); // filter noframes labels $ descclear = preg_replace ("/<(\/? Noframes. *?)> /Si "," ", $ descclear); // filter noframes labels $ descclear = preg_replace ("/<(I? Frame. *?)> (.*?) <(\/I? Frame. *?)> /Si "," ", $ descclear); // filter the frame tag $ descclear = preg_replace ("/<(\/? I? Frame. *?)> /Si "," ", $ descclear); // filter the frame tag $ descclear = preg_replace ("/<(script. *?)> (.*?) <(\/Script. *?)> /Si "," ", $ descclear); // filter the script tag $ descclear = preg_replace ("/<(\/? Script. *?)> /Si "," ", $ descclear); // filter the script tag $ descclear = preg_replace ("/javascript/si "," Javascript ", $ descclear ); // filter the script tag $ descclear = preg_replace ("/vbscript/si", "Vbscript", $ descclear ); // filter the script tag $ descclear = preg_replace ("/on ([a-z] +) \ s * =/si", "On \ 1 = ", $ descclear); // filter the script tag $ descclear = preg_replace ("/& #/si", "& #", $ descclear); // filter the script tag, such as javAsCript: alert (); // replace $ pat = "/with regular expressions <(\/?) (Script | I? Frame | style | html | body | li | I | map | title | img | link | span | u | font | table | tr | B | marquee | td | strong | p | a | meta | \? | \ %) ([^>] *?)> /IsU "; $ descclear = preg_replace ($ pat," ", $ descclear); return $ descclear;?>

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.