Discuz!大师网

搜索
查看: 1025|回复: 0

Discuz!字符串裁剪函数cutstr介绍

[复制链接]
发表于 2017-4-7 01:36:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x

函数定义位置:
\source\function\function_core.php
函数定义:
  1. function cutstr($string, $length, $dot = ' ...') {
  2.         if(strlen($string) <= $length) {
  3.                 return $string;
  4.         }

  5.         $pre = chr(1);
  6.         $end = chr(1);
  7.         $string = str_replace(array('&', '"', '<', '>'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);

  8.         $strcut = '';
  9.         if(strtolower(CHARSET) == 'utf-8') {

  10.                 $n = $tn = $noc = 0;
  11.                 while($n < strlen($string)) {

  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) {
  29.                                 break;
  30.                         }

  31.                 }
  32.                 if($noc > $length) {
  33.                         $n -= $tn;
  34.                 }

  35.                 $strcut = substr($string, 0, $n);

  36.         } else {
  37.                 $_length = $length - 1;
  38.                 for($i = 0; $i < $length; $i++) {
  39.                         if(ord($string[$i]) <= 127) {
  40.                                 $strcut .= $string[$i];
  41.                         } else if($i < $_length) {
  42.                                 $strcut .= $string[$i].$string[++$i];
  43.                         }
  44.                 }
  45.         }

  46.         $strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&', '"', '<', '>'), $strcut);

  47.         $pos = strrpos($strcut, chr(1));
  48.         if($pos !== false) {
  49.                 $strcut = substr($strcut,0,$pos);
  50.         }
  51.         return $strcut.$dot;
  52. }
复制代码

使用说明
cutstr($string, $length, $dot = ' ...')
$string:要裁剪的字符串
$length:返回长度
$dot:替换截掉部分的结尾字符串

回复 马甲回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|网站地图|小黑屋|展会网|Discuz站长论坛 |天天打卡

GMT+8, 2024-4-29 18:33 , Processed in 0.026582 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表