使用php實現(xiàn)截取指定長度
更新時間:2013年08月06日 09:18:13 作者:
以下是對使用php實現(xiàn)截取指定長度的方法進行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
PHP語言: 根據(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 array_chunk()函數(shù)用法與注意事項
這篇文章主要介紹了php array_chunk()函數(shù)用法與注意事項,結(jié)合實例形式分析了php數(shù)組分割函數(shù)array_chunk()相關(guān)功能、用法及操作注意事項,需要的朋友可以參考下2019-07-07php獲取flash尺寸詳細(xì)數(shù)據(jù)的方法
這篇文章主要介紹了php獲取flash尺寸詳細(xì)數(shù)據(jù)的方法,涉及php getimagesize函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2016-11-11Ubuntu中搭建Nginx、PHP環(huán)境最簡單的方法
這篇文章主要介紹了Ubuntu中搭建Nginx、PHP環(huán)境最簡單的方法,本文講解的是使用apt-get工具安裝Nginx、PHP環(huán)境,并講解了基本配置,需要的朋友可以參考下2015-03-03PHP中函數(shù)rand和mt_rand的區(qū)別比較
mt_rand() 比rand() 快四倍,很多老的 libc 的隨機數(shù)發(fā)生器具有一些不確定和未知的特性而且很慢。PHP 的 rand() 函數(shù)默認(rèn)使用 libc 隨機數(shù)發(fā)生器。mt_rand() 函數(shù)是非正式用來替換它的。2012-12-12在PHP上顯示JFreechart畫的統(tǒng)計圖方法
在JSP上的servlet能完全的顯示出JFreechart畫的統(tǒng)計圖,但是和其他語言混合運用就不能顯示了,下面為大家介紹下如何在PHP上顯示JFreechart2013-11-11