复制代码 代码如下:
<?
$str = '都知道strlen与mb_strlen是求字符串长度的函数';
echo strlen($str)'.<br />'.mb_strlen($str,'utf-8');
?>
复制代码 代码如下:
function cutstr($sourcestr,$cutlength){
$returnstr = '';
$i = 0;
$n = 0;
$str_length = strlen($sourcestr);
$mb_str_length = mb_strlen($sourcestr,'utf-8');
while(($n < $cutlength) && ($i <= $str_length)){
$temp_str = substr($sourcestr,$i,1);
$ascnum = ord($temp_str);
if($ascnum >= 224){
$returnstr = $returnstr.substr($sourcestr,$i,3);
$i = $i + 3;
$n++;
}
elseif($ascnum >= 192){
$returnstr = $returnstr.substr($sourcestr,$i,2);
$i = $i + 2;
$n++;
}
elseif(($ascnum >= 65) && ($ascnum <= 90)){
$returnstr = $returnstr.substr($sourcestr,$i,1);
$i = $i + 1;
$n++;
}
else{
$returnstr = $returnstr.substr($sourcestr,$i,1);
$i = $i + 1;
$n = $n + 0.5;
}
}
if ($mb_str_length > $cutlength){
$returnstr = $returnstr . "...";
}
return $returnstr;
}
复制代码 代码如下:
<?
$str = '有效期最长三个月,超过有效期系统将自动删除本条信息';
//echo strlen($str);
//echo '<hr />'.mb_strlen($str,'utf-8');
echo '<hr />'.$str;
echo '<hr />'.cutstr($str,24);
?>
新闻热点
疑难解答