广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 3878 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为IE收藏   收藏主题   上一主题 | 下一主题   
Qoo 手机 会员卡
数位造型
个人文章 个人相簿 个人日记 个人地图
头衔:超人超人

级别: 副版主 该用户目前不上站
版区: 星侨五术软体
推文 x22 鲜花 x126
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片
推文 x0
[PHP][原码] 未安装 mbstring 之 UTF-8 字串长度处理
这个可以用来截取 UTF-8 的字串..
此方法适用于没有安装 mbstring 函数的主机

复制程式
function cutstr($string, $length, $havedot=0) {

//判断长度
if(strlen($string) <= $length) {
  return $string;
}
$wordscut = ''
if(strtolower($charset) == 'utf-8') {
  //utf8编码
  $n = 0;
  $tn = 0;
  $noc = 0;
  while ($n < strlen($string)) {
   $t = ord($string[$n]);
   if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
    $tn = 1;
    $n++;
    $noc++;
   } elseif(194 <= $t && $t <= 223) {
    $tn = 2;
    $n += 2;
    $noc += 2;
   } elseif(224 <= $t && $t < 239) {
    $tn = 3;
    $n += 3;
    $noc += 2;
   } elseif(240 <= $t && $t <= 247) {
    $tn = 4;
    $n += 4;
    $noc += 2;
   } elseif(248 <= $t && $t <= 251) {
    $tn = 5;
    $n += 5;
    $noc += 2;
   } elseif($t == 252 || $t == 253) {
    $tn = 6;
    $n += 6;
    $noc += 2;
   } else {
    $n++;
   }
   if ($noc >= $length) {
    break;
   }
  }
  if ($noc > $length) { 
   $n -= $tn;
  }
  $wordscut = substr($string, 0, $n);
} else {
  for($i = 0; $i < $length - 3; $i++) {
   if(ord($string[$i]) > 127) {
    $wordscut .= $string[$i].$string[$i + 1];
    $i++;
   } else {
    $wordscut .= $string[$i];
   }
  }
}
//省略号
if($havedot) {
  return $wordscut.'...'
} else {
  return $wordscut;
}
} 


[ 此文章被Qoo在2007-02-26 16:55重新编辑 ]



献花 x0 回到顶端 [楼 主] From:台湾新世纪资通股份有限公司 | Posted:2007-02-26 16:04 |

首页  发表文章 发表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.015177 second(s),query:15 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言