php截取并生成纯文本字符串。最近使用文本编辑器保存的内容中有html,css以及js标记,但是我只想截取前面一部分纯文本内容显示摘要,这里直接上代码,如下:
/**
* PHP清除html、css、js格式并去除空格的PHP函数,并具有截取UTF-8字符串的作用
*/
function cutstr_html($string, $sublen){
$string = strip_tags($string);
$string = preg_replace ('/\n/is', '', $string);
$string = preg_replace ('/ | /is', '', $string);
$string = preg_replace ('/ /is', '', $string);
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, $t_string);
if(count($t_string[0]) - 0 > $sublen) $string = join('', array_slice($t_string[0], 0, $sublen))."…";
else $string = join('', array_slice($t_string[0], 0, $sublen));
return $string;
}
希望对大家有用
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。