php如何清除html格式并去除文字中的空格然后截取文字

如题所述

PHP清除html、css、js格式并去除空格的PHP函数

01 function cutstr_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('/ /is','',$string);
06 preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/",$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 去除html标签 js 和 css样式

01 function clearHtml($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 }
温馨提示:答案为网友推荐,仅供参考