首页 > CMS建站 > PhpCMS > 正文

Phpcms V9截取字符函数改进:截取英文更精确

2020-10-10 21:17:10
字体:
来源:转载
供稿:网友

今天在这里分享最近在英文网站中优化的一个小改进,Phpcms V9截取字符函数改进,截取英文更精确,具体方法来自PC官方论坛,分享在此.

官方默认的str_cut()截取的字符统计长度不精准,特别是在中英文字符混搭的时候,调用列表会出现问题,采用【拽拽焱】分享的方法,可以将长度偏差在2个字符内,很不错,先赞一个.

具体代码:

  1. /** 
  2.  * 字符截取 支持UTF8/GBK 
  3.  * @param $string 
  4.  * @param $length 
  5.  * @param $dot 
  6.  */ 
  7. function str_cut($string$length$dot = '') { 
  8.         $strlen = strlen($string); 
  9.         if($strlen/2 <= $lengthreturn $string
  10.         $string = str_replace(array(' ','        ',' ',' ''&''"'''', '', '', '', '<', '>', '·', ''), array(' ',' ',' ',' ', '&', '"', "'", '“''”''—''<''>''·''…'), $string); 
  11.         $strcut = ''
  12.         $n = $tn = $noc = 0; 
  13.         if(strtolower(CHARSET) == 'utf-8') { 
  14.                 while($n < $strlen) { 
  15.                         $t = ord($string[$n]); 
  16.                         if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { 
  17.                                 $tn = 1; ++$n$noc += 0.5; 
  18.                         } elseif(194 <= $t && $t <= 223) { 
  19.                                 $tn = 2; $n += 2; $noc += 1; 
  20.                         } elseif(224 <= $t && $t <= 239) { 
  21.                                 $tn = 3; $n += 3; $noc += 1; 
  22.                         } elseif(240 <= $t && $t <= 247) { 
  23.                                 $tn = 4; $n += 4; $noc += 1; 
  24.                         } elseif(248 <= $t && $t <= 251) { 
  25.                                 $tn = 5; $n += 5; $noc += 1; 
  26.                         } elseif($t == 252 || $t == 253) { 
  27.                                 $tn = 6; $n += 6; $noc += 1; 
  28.                         } else { 
  29.                                 ++$n
  30.                         } 
  31.                         if($noc >= $length) { 
  32.                                 if($n < $strlen) ++$noc
  33.                                 break
  34.                         } 
  35.                 } 
  36.         } else {                 
  37.                 while($n < $strlen) { 
  38.                         if(ord($string[$n]) > 127) { 
  39.                                 $tn = 2; $n += 2; $noc += 1; 
  40.                         } else
  41.                                 $tn = 1; ++$n$noc += 0.5; 
  42.                         } 
  43.                         if($noc >= $length) { 
  44.                                 if($n < $strlen) ++$noc
  45.                                 break
  46.                         } 
  47.                 } 
  48.         } 
  49.         if($noc > $length && !emptyempty($dot)) { 
  50.                 $n -= $tn
  51.                 $strcut = substr($string, 0, $n); 
  52.                 $strcut .= $dot
  53.         }else
  54.                 $strcut = substr($string, 0, $n); 
  55.         }  //Cuoxin.com 
  56.         $strcut = str_replace(array('&''"', "'", '', '', '', '<', '>', '·', ''), array('&', '"', ''''“''”''—''<''>''·''…'), $strcut); 
  57.         return $strcut
  58. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表