天天看点

php 文本操作类

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

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

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