当前位置:首页 > CMS教程 > phpcms > 列表

phpcms v9 修改系统函数str_cut()

发布:smiling 来源: PHP粉丝网  添加日期:2014-10-21 15:40:07 浏览: 评论:0 
  1. function str_cut($string, $length, $dot = '') 
  2. { 
  3.  $strlen = strlen($string); 
  4.  if($strlen <= $length) return $string; 
  5.  $string = str_replace(array('&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array(' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string); 
  6.  $strcut = ''; 
  7.  if(strtolower(CHARSET) == 'utf-8') 
  8.  { 
  9.   $n = $tn = $noc = 0; 
  10.   while($n < $strlen) 
  11.   { 
  12.    $t = ord($string[$n]); 
  13.    if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { 
  14.     $tn = 1; $n++; $noc++; 
  15.    } elseif(194 <= $t && $t <= 223) { 
  16.     $tn = 2; $n += 2; $noc += 2; 
  17.    } elseif(224 <= $t && $t < 239) { 
  18.     $tn = 3; $n += 3; $noc += 2; 
  19.    } elseif(240 <= $t && $t <= 247) { 
  20.     $tn = 4; $n += 4; $noc += 2; 
  21.    } elseif(248 <= $t && $t <= 251) { 
  22.     $tn = 5; $n += 5; $noc += 2; 
  23.    } elseif($t == 252 || $t == 253) { 
  24.     $tn = 6; $n += 6; $noc += 2; 
  25.    } else { 
  26.     $n++; 
  27.    } 
  28.    if($noc >= $length) break; 
  29.   } 
  30.   if($noc > $length) $n -= $tn; 
  31.   $strcut = substr($string, 0, $n); 
  32.  } 
  33.  else 
  34.  { 
  35.   $dotlen = strlen($dot); 
  36.   $maxi = $length - $dotlen - 1; 
  37.   for($i = 0; $i < $maxi; $i++) 
  38.   { 
  39.    $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; 
  40.   }  //phpfensi.com 
  41.  } 
  42.  $strcut = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&#039;', '&lt;', '&gt;'), $strcut); 
  43.  return $strcut.$dot; 
  44. }

Tags: phpcms系统函数 str_cut()系统函数

分享到: