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

Source: Internet
Author: User

This article introduces various methods and implementation programs for removing unnecessary HTML, Javascrit, and Css labels from PHP.

1. Do not retain any HTML tags. The Code will be like this: echo strip_tags ($ str );

2. if you keep only <p> one tag, you only need to write the <p> string to the second parameter of strip_tags. The Code will be like this: echo strip_tags ($ str, "<p> ");

3. We need to keep <p> and <B>... Multiple tags. You only need to separate multiple tags with spaces and write them to the second parameter of strip_tags. The Code will be like this: echo strip_tags ($ str, "<p> <B> ");

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 Code is as follows: Copy code


<? Php
$ New = htmlspecialchars ("<a href = 'test'> test </a>", ENT_QUOTES );
Echo $ new;

?>

Convert special characters into the string format of HTML (&....;). The most common use case may be the message board for handling customer messages.

& (And) to & amp;
"(Double quotation marks) to & quot;
<(Less than) to & lt;
> (Greater than) to & gt;
This function only converts the preceding special characters and does not convert all the characters to the ASCII conversion specified in HTML.

 

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

The Code is as follows: Copy code

Function get_enhtml ($ string ){
$ Pattern = array ("'<script [^>] *?>. *? </Script> 'si ", // remove javascript
"'<Style [^>] *?>. *? </Style> 'si ", // remove the HTML Tag
"'<[/!] *? [^ <>] *?> 'Si ", // remove the HTML Tag
"'<! -- [/!] *? [^ <>] *?> 'Si ", // remove the annotation mark
"'([Rn]) [s] +'", // remove the white space
"'& (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 )");
$ String = preg_replace ($ pattern, $ replace, $ string );
$ String = preg_replace ("/<(. *?)> /"," ", $ String );
$ String = str_replace ("n", "", $ string );
$ String = str_replace ("r", "", $ string );
$ String = str_replace ("", "", $ string );
$ String = str_replace ("", "", $ string );
Return addslashes (trim ($ string ));
}

Replace all html tags

The Code is as follows: Copy code

Function noHTML ($ content)
{
$ Content = preg_replace ("/<a [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</a>/I", '', $ content );
$ Content = preg_replace ("/<div [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</div>/I", '', $ content );
$ Content = preg_replace ("/<font [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</font>/I", '', $ content );
$ Content = preg_replace ("/<p [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</p>/I", '', $ content );
$ Content = preg_replace ("/<span [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</span>/I", '', $ content );
$ Content = preg_replace ("/<? Xml [^>] *>/I ",'', $ content );
$ Content = preg_replace ("/</? Xml>/I ",'', $ content );
$ Content = preg_replace ("/<o: p [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</o: p>/I", '', $ content );
$ Content = preg_replace ("/<u [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</u>/I", '', $ content );
$ Content = preg_replace ("/<B [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</B>/I", '', $ content );
$ Content = preg_replace ("/<meta [^>] *>/I", '', $ content );
$ Content = preg_replace ("/</meta>/I", '', $ content );
$ Content = preg_replace ("/<! -- [^>] * -->/I ",'', $ content); // comment content
$ Content = preg_replace ("/<p [^>] * -->/I", '', $ content); // comment content
$ Content = preg_replace ("/style =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/class =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/id =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/lang =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/width =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/height =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/border =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/face =. +? ['| "]/I", '', $ content); // remove the style
$ Content = preg_replace ("/face =. +? ['| "]/", '', $ Content );
$ Content = preg_replace ("/face =. +? ['| "]/", '', $ Content );
$ Content = str_replace ("& nbsp;", "", $ content );
Return $ content;
}

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.