使用php實(shí)現(xiàn)截取指定長(zhǎng)度
更新時(shí)間:2013年08月06日 09:18:13 作者:
以下是對(duì)使用php實(shí)現(xiàn)截取指定長(zhǎng)度的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下
PHP語(yǔ)言: 根據(jù)中文裁減字符串
function cutstr($string,$length, $dot = ' ...') {
if (strlen($string) <= $length) {
return$string;
}
$pre = chr(1);
$end = chr(1);
$string = str_replace(array('&','"', '<', '>'),array($pre . '&'. $end, $pre . '"' . $end, $pre . '<' .$end, $pre . '>' . $end), $string);
$strcut = '';
if (strtolower(CHARSET) == 'utf-8'){
$n = $tn =$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;
}
$strcut =substr($string, 0, $n);
} else {
for ($i =0; $i < $length; $i++) {
$strcut .= ord($string[$i])> 127 ? $string[$i] . $string[++$i] : $string[$i];
}
}
$strcut = str_replace(array($pre . '&' .$end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' .$end), array('&', '"','<', '>'), $strcut);
$pos = strrpos($strcut, chr(1));
if ($pos !== false) {
$strcut =substr($strcut, 0, $pos);
}
return $strcut . $dot;
}
復(fù)制代碼 代碼如下:
function cutstr($string,$length, $dot = ' ...') {
if (strlen($string) <= $length) {
return$string;
}
$pre = chr(1);
$end = chr(1);
$string = str_replace(array('&','"', '<', '>'),array($pre . '&'. $end, $pre . '"' . $end, $pre . '<' .$end, $pre . '>' . $end), $string);
$strcut = '';
if (strtolower(CHARSET) == 'utf-8'){
$n = $tn =$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;
}
$strcut =substr($string, 0, $n);
} else {
for ($i =0; $i < $length; $i++) {
$strcut .= ord($string[$i])> 127 ? $string[$i] . $string[++$i] : $string[$i];
}
}
$strcut = str_replace(array($pre . '&' .$end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' .$end), array('&', '"','<', '>'), $strcut);
$pos = strrpos($strcut, chr(1));
if ($pos !== false) {
$strcut =substr($strcut, 0, $pos);
}
return $strcut . $dot;
}
相關(guān)文章
PHP中使用Session配合Javascript實(shí)現(xiàn)文件上傳進(jìn)度條功能
這篇文章主要介紹了PHP中使用Session配合Javascript實(shí)現(xiàn)文件上傳進(jìn)度條功能,本文講解了實(shí)現(xiàn)原理,并給出了完整實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-10-10
php變量與JS變量實(shí)現(xiàn)不通過(guò)跳轉(zhuǎn)直接交互的方法
這篇文章主要介紹了php變量與JS變量實(shí)現(xiàn)不通過(guò)跳轉(zhuǎn)直接交互的方法,涉及php與javascript變量傳輸及代碼混編相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
PHP中addcslashes與stripcslashes函數(shù)用法分析
這篇文章主要介紹了PHP中addcslashes與stripcslashes函數(shù)用法,結(jié)合實(shí)例形式分析了addcslashes函數(shù)添加針對(duì)固定字符的轉(zhuǎn)義處理與stripcslashes進(jìn)行還原操作相關(guān)技巧,需要的朋友可以參考下2016-01-01
php MsSql server時(shí)遇到的中文編碼問(wèn)題
朋友要用sugarcrm的php讀取Ms sql server的中文資料,因?yàn)槠湓假Y料是Access 數(shù)據(jù)庫(kù),導(dǎo)到mysql不太方便。2009-06-06
PHP數(shù)組與對(duì)象之間使用遞歸實(shí)現(xiàn)轉(zhuǎn)換的方法
這篇文章主要介紹了PHP數(shù)組與對(duì)象之間使用遞歸實(shí)現(xiàn)轉(zhuǎn)換的方法,涉及php數(shù)組與對(duì)象的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06
使用array mutisort 實(shí)現(xiàn)按某字段對(duì)數(shù)據(jù)排序
本篇文章是對(duì)使用array mutisort 實(shí)現(xiàn)按某字段對(duì)數(shù)據(jù)排序的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP 刪除文件與文件夾操作 unlink()與rmdir()這兩個(gè)函數(shù)的使用
有時(shí)候我們需要用PHP來(lái)刪除文件和文件夾,PHP本來(lái)也都有函數(shù)可以實(shí)現(xiàn),下面簡(jiǎn)單記錄一下代碼,方便以后信守拈來(lái)。2011-07-07

