发一段自己整理的文本操作类

目前包含常用文本字符串无乱码截取,词意化时间,以及敏感词过滤(其中敏感词过滤需自己添加敏感词库类似于

 

 
  
  1. $config ['sensitive_words'] = array ( 
  2.         '¤李刚'
  3.         '西白'
  4.         '双奇还源丹'
  5.         '伟达',); 

我这里因为在ci中使用,写到了敏感词配置文件中,如需他用请自行修改

最后上源码,不断完善中

 

 
  
  1. <?php 
  2. /** 
  3.  * 提出文章摘要 
  4.  * @author liyi 
  5.  * @version 1.1 
  6.  * @since 10/07 
  7.  */ 
  8. class Articleoper { 
  9.     private static $_string = ''
  10.     /** 
  11.      * 宽字符串截字函数 
  12.      * 
  13.      * @access public 
  14.      * @param string $str 需要截取的字符串 
  15.      * @param integer $start 开始截取的位置 
  16.      * @param integer $length 需要截取的长度 
  17.      * @param string $trim 截取后的截断标示符 
  18.      * @param string $charset 字符串编码 
  19.      * @return string 
  20.      */ 
  21.     public static function subStr($str$start$length$trim = "..."$charset = 'UTF-8'
  22.     { 
  23.         if (function_exists('mb_get_info'))  
  24.         { 
  25.             $iLength = mb_strlen($str$charset); 
  26.             $str = mb_substr($str$start$length$charset); 
  27.              
  28.             return ($length < $iLength - $start) ? $str . $trim : $str
  29.         }  
  30.         else  
  31.         { 
  32.             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]/"$str$info); 
  33.             $str = join(""array_slice($info[0], $start$length)); 
  34.              
  35.             return ($length < (sizeof($info[0]) - $start)) ? $str . $trim : $str
  36.         } 
  37.     }    
  38.      
  39.      
  40.     /** 
  41.     * 词义化时间 
  42.     *  
  43.     * @access public 
  44.     * @param string $from 起始时间 
  45.     * @param string $now 终止时间 
  46.     * @return string 
  47.     */ 
  48.     public static function dateWord($from$now
  49.     { 
  50.         //fix issue 3#6 by saturn, solution by zycbob 
  51.          
  52.         /** 如果不是同一年 */ 
  53.         if (idate('Y'$now) != idate('Y'$from))  
  54.         { 
  55.             return date('y年m月d日'$from); 
  56.         } 
  57.          
  58.         /** 以下操作同一年的日期 */ 
  59.         $seconds = $now - $from
  60.         $days = idate('z'$now) - idate('z'$from); 
  61.          
  62.         /** 如果是同一天 */ 
  63.         if ($days == 0)  
  64.         { 
  65.             /** 如果是一小时内 */ 
  66.             if ($seconds < 3600)  
  67.             { 
  68.                 /** 如果是一分钟内 */ 
  69.                 if ($seconds < 60) 
  70.                 { 
  71.                     if (3 > $seconds)  
  72.                     { 
  73.                         return '刚刚'
  74.                     }  
  75.                     else  
  76.                     { 
  77.                         return sprintf('%d秒前'$seconds); 
  78.                     } 
  79.                 } 
  80.  
  81.                 return sprintf('%d分钟前'intval($seconds / 60)); 
  82.             } 
  83.  
  84.             return sprintf('%d小时前', idate('H'$now) - idate('H'$from)); 
  85.         } 
  86.  
  87.         /** 如果是昨天 */ 
  88.         if ($days == 1)  
  89.         { 
  90.             return sprintf('昨天 %s'date('H:i'$from)); 
  91.         } 
  92.          
  93.         /** 如果是前天 */ 
  94.         if ($days == 2)  
  95.         { 
  96.             return sprintf('前天 %s'date('H:i'$from)); 
  97.         } 
  98.  
  99.         /** 如果是7天内 */ 
  100.         if ($days < 7)  
  101.         { 
  102.             return sprintf('%d天前'$days); 
  103.         } 
  104.  
  105.         /** 超过一周 */ 
  106.         return date('n月j日'$from); 
  107.     } 
  108.      
  109.     /** 
  110.      * 敏感词过滤或检查 
  111.      * @param string         $string        要过滤的字符串 
  112.      * @param string         $tostring      配置过滤后的替换字符 
  113.      * @param bool           $justCheck     是否只是检查含有敏感词,如果有返回false 
  114.      * @return                 过滤后的字符串或bool值(如果只是检查字符串中是否含有敏感词) 
  115.      */ 
  116.     public static function filterWords($string,$tostring = '<span style="color:blue">"敏感词,已被过滤"</span>',$justCheck = FALSE) 
  117.     { 
  118.         if (isset($string)) 
  119.         { 
  120.             $CI = & get_instance(); 
  121.             $CI->load->config('sensitive_words'); 
  122.             //读取敏感词库 
  123.             $filter = $CI->config->item('sensitive_words'); 
  124.             //初始化要过滤的词 
  125.             self::$_string = $string
  126.             if (!$justCheck
  127.             { 
  128.                 //替换敏感词 
  129.                 self::$_string = str_replace(array_values($filter), $tostring, self::$_string); 
  130.             } else { 
  131.                 //如果设置为只检查是否含有敏感词,循环敏感词库查找是否含有敏感词 
  132.                 foreach ($filter as $key=>$value
  133.                 { 
  134.                     $checked = strpos($string,$value); 
  135.                     if ($checked)  
  136.                     { 
  137.                         self::$_string = FALSE;//含有敏感词,返回false 
  138.                         continue
  139.                     } 
  140.                 } 
  141.             } 
  142.         } 
  143.         return self::$_string
  144.     } 
  145. ?> 
  146. 使用:
    Articleoper::subStr();