<?php
/*
用途:計算顯示長度
Author: Ron Lee
Date: 20150805
*/
function strlen_utf8_show($str){
// Ron L
$count = 0;//累計顯示長度
$strLen = strlen($str);//原始字串長度
$accStrLen = 0; //累計字串長度
$accShowLen = 0; //累計顯示長度
$currStrLenPos = 0; //當前字符的起始位置 (byte)
$currChrNum = 0; //第幾個字符
$currChr = ''; //當前字元
while($currStrLenPos < $strLen){
$currChr = mb_substr($str,$currChrNum,1,'utf8');
$asciiChr = ord($currChr);
if($asciiChr<128){
$thisChrLen = 1;//字串長度
$thisChrShowLen = 1;//字串顯示長度
}
elseif($asciiChr<192){
//no data
continue;
}
elseif($asciiChr<224){
$thisChrLen = 2;//字串長度
$thisChrShowLen = 2;//字串顯示長度
}
elseif($asciiChr<240){
$thisChrLen = 3;//字串長度
$thisChrShowLen = 2;//字串顯示長度
}
elseif($asciiChr<248){
$thisChrLen = 4;//字串長度
$thisChrShowLen = 2;//字串顯示長度
}
elseif($asciiChr<252){
$thisChrLen = 5;//字串長度
$thisChrShowLen = 1;//字串顯示長度
}
elseif($asciiChr<254){
$thisChrLen = 6;//字串長度
$thisChrShowLen = 1;//字串顯示長度
}
$accStrLen += $thisChrLen;
$accShowLen += $thisChrShowLen;
$currStrLenPos += $thisChrLen;
$currChrNum ++;
}
return $accShowLen;
}
?>
2013年12月5日 星期四
PHP:計算顯示長度
程式碼:
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言