PHP removes unnecessary HTML, Javascrit, and Css labels. This article introduces various methods and implementation programs for removing unnecessary HTML, Javascrit, and Css labels from PHP. 1. without retaining any HTML tags, the code will be like this: This article will introduce various methods and implementation programs for removing unnecessary HTML, HTML crit, and Css tags from PHP. for details, refer.
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. 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 ,"
");
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: |
|
$ New = htmlspecialchars ("Test", 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) &
"(Double quotation marks)"
<(Less than) to <
> (Greater than) to>
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: |
|
Function get_enhtml ($ string ){ $ Pattern = array ("' ] *?>. *? Script 'Si ", // remove javascript "' ] *?>. *?'Si ", // remove the HTML tag "'<[/!] *? [^ <>] *?> 'Si ", // remove the HTML tag "' /I ",'', $ content); // comment the content $ Content = preg_replace ("/ ] * -->/I ",'', $ content); // comment the 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 ("", "", $ content ); Return $ content; } |
Refer to the pipeline method and implementation program. 1. do not retain any HTML tags. the code will look like this :...