控制标题字数的函数是怎样的呢?
现在帮公司首页插入一个新闻列表,表格太小呀,文章标题太长了,就换行了 ,非常影响美观..
现在想实现标题字数限制.如: XXXXXXXXX... 达到了设定的字数,就用省略号替换的形式..
请熟悉PHP函数的朋友提供控制标题字符数的函数....急了....
现在想实现标题字数限制.如: XXXXXXXXX... 达到了设定的字数,就用省略号替换的形式..
请熟悉PHP函数的朋友提供控制标题字符数的函数....急了....
作者: jianjin 发布时间: 2006-11-02
先判断标题 超过指定长度就截取指定长度 然后结尾加上.....
作者: 17521 发布时间: 2006-11-03
参数 [要截取的字符串][截取长度][结尾附加的内容 默认附加...]
CODE:
[Copy to clipboard]
<?php
function cutstr($string, $length, $dot = ' ...') {
global $charset;
if(strlen($string) <= $length) {
return $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 - strlen($dot) - 1; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
return $strcut.$dot;
}
?>
function cutstr($string, $length, $dot = ' ...') {
global $charset;
if(strlen($string) <= $length) {
return $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 - strlen($dot) - 1; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
return $strcut.$dot;
}
?>
作者: 17521 发布时间: 2006-11-03
我写的函数:
//切割定长标题(适用于非UTF编码)
function cutTitle($str, $len, $tail = ""){
$length = strlen($str);
$lentail = strlen($tail);
$result = "";
if($length > $len){
$len = $len - $lentail;
for($i = 0;$i < $len;$i ++){
if(ord($str[$i]) < 127){
$result .= $str[$i];
}else{
$result .= $str[$i];
++ $i;
$result .= $str[$i];
}
}
$result = strlen($result) > $len ? substr($result, 0, -2) . $tail : $result . $tail;
}else{
$result = $str;
}
return $result;
}
再给你一个discuz写的函数:
function cutstr($string, $length) {
$strcut = '';
if(strlen($string) > $length) {
for($i = 0; $i < $length - 3; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
return $strcut.' ...';
} else {
return $string;
}
}
//切割定长标题(适用于非UTF编码)
function cutTitle($str, $len, $tail = ""){
$length = strlen($str);
$lentail = strlen($tail);
$result = "";
if($length > $len){
$len = $len - $lentail;
for($i = 0;$i < $len;$i ++){
if(ord($str[$i]) < 127){
$result .= $str[$i];
}else{
$result .= $str[$i];
++ $i;
$result .= $str[$i];
}
}
$result = strlen($result) > $len ? substr($result, 0, -2) . $tail : $result . $tail;
}else{
$result = $str;
}
return $result;
}
再给你一个discuz写的函数:
function cutstr($string, $length) {
$strcut = '';
if(strlen($string) > $length) {
for($i = 0; $i < $length - 3; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
return $strcut.' ...';
} else {
return $string;
}
}
作者: sanders_yao 发布时间: 2006-11-03