廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 3880 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為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.013926 second(s),query:15 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言