How PHP clears HTML formatting and removes whitespace from text and then intercepts text

Source: Internet
Author: User

PHP How to clear the HTML format and remove the space in the text and then intercept the text, a detailed share of the processing method (by the way PHP clears the HTML string function to do a summary):

Htmlspecialchars Turning special characters into HTML
Format syntax: String Htmlspecialchars (String string);
return value: String
Function type: Data processing
Content Description This function converts special characters to HTML string format (&....;).
The most commonly used occasion may be to deal with the message of the customer message version.
& (and) turn into &
"(double quotation marks) turn into "
< (less than) turn into &lt;
> (greater than) turn into &gt;
This function converts only the above special characters, and does not convert all of the ASCII conversions that are determined by the HTML.

htmlentities All characters into an HTML string
Syntax: String htmlentities (String string);
return value: String
Function type: Data processing
Content Description The function is somewhat like a htmlspecialchars () function, but the function converts all string characters to the special word set of the HTML character string. However, after the conversion to read the page source code, there will be a lot of trouble, especially the text in the original code will become unintelligible, the browser to see the normal.

Strip_tags ($STR) Remove markup syntax for HTML and PHP: String strip_tags (String str); Return value: String function type: Data processing content Description: This function can remove any HTML and PHP tag strings contained in the string. If the string of HTML and PHP tags are wrong, for example, less than the symbol, it will also return errors.

PHP to remove HTML, CSS style, JS format a lot of methods, but based on experience found that they basically have a drawback: basically all the text contained in the Space or tab, after continuous exploration, finally found an ideal to remove HTML characters and can remove space, CSS style and JS PHP functions.

PHP to clear HTML, CSS, JS format and remove whitespace php function

View Code Printing
01 functioncutstr_html($string,$length=0,$ellipsis=‘…‘){
02     $string=strip_tags($string);
03     $string=preg_replace(‘/\n/is‘,‘‘,$string);
04     $string=preg_replace(‘/ | /is‘,‘‘,$string);
05     $string=preg_replace(‘/&nbsp;/is‘,‘‘,$string);
"    ;    preg_match_all ( $string $string
07     if(is_array($string)&&!empty($string[0])){
08         if(is_numeric($length)&&$length){
09             $string=join(‘‘,array_slice($string[0],0,$length)).$ellipsis;
10         }else{
11             $string=implode(‘‘,$string[0]);
12         }
13     }else{
14         $string=‘‘;
15     }
16     return$string;
17 }

PHP removes HTML tag js and CSS Styles

View Code Printing
01 functionclearHtml($content){
02     $content=preg_replace("/<a[^>]*>/i","",$content);
03     $content=preg_replace("/<\/a>/i","",$content);
04     $content=preg_replace("/<div[^>]*>/i","",$content);
05     $content=preg_replace("/<\/div>/i","",$content);
06     $content=preg_replace("/<!--[^>]*-->/i","",$content);//注释内容    
07     $content=preg_replace("/style=.+?[‘|\"]/i",‘‘,$content);//去除样式    
08     $content=preg_replace("/class=.+?[‘|\"]/i",‘‘,$content);//去除样式    
09     $content=preg_replace("/id=.+?[‘|\"]/i",‘‘,$content);//去除样式       
10     $content=preg_replace("/lang=.+?[‘|\"]/i",‘‘,$content);//去除样式        
11     $content=preg_replace("/width=.+?[‘|\"]/i",‘‘,$content);//去除样式     
12     $content=preg_replace("/height=.+?[‘|\"]/i",‘‘,$content);//去除样式     
13     $content=preg_replace("/border=.+?[‘|\"]/i",‘‘,$content);//去除样式     
14     $content=preg_replace("/face=.+?[‘|\"]/i",‘‘,$content);//去除样式     
15     $content=preg_replace("/face=.+?[‘|\"]/",‘‘,$content);//去除样式 只允许小写 正则匹配没有带 i 参数  
16     return$content;
17 }

How PHP clears HTML formatting and removes whitespace from text and then intercepts text

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.