首页 > Web开发 > 详细

PHP截取并生成纯文本字符串

时间:2017-07-12 12:01:25      阅读:390      评论:0      收藏:0      [点我收藏+]

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;
 }

希望对大家有用

技术分享技术分享

本文出自 “高万耀” 博客,请务必保留此出处http://gaowanyao.blog.51cto.com/11272977/1946484

PHP截取并生成纯文本字符串

原文:http://gaowanyao.blog.51cto.com/11272977/1946484

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!