$s="";
$cheng="";while($myrow=mysql_fetch_row($result))
{
$s=$myrow[0];
if(strlen($s)<9)
{?><a class=black12><?=$s?></a><? }
else
{
$cheng=substr($s,0,9);
print("<a class=black12 title=".$s.">".$cheng."...</a>");
$cheng="";}
?>
$cheng="";while($myrow=mysql_fetch_row($result))
{
$s=$myrow[0];
if(strlen($s)<9)
{?><a class=black12><?=$s?></a><? }
else
{
$cheng=substr($s,0,9);
print("<a class=black12 title=".$s.">".$cheng."...</a>");
$cheng="";}
?>
CODE:
[Copy to clipboard]
<?php
<?PHP
$str="这个字符好长呀,^_^";
$Short_Str=showShort($str,4);//截取前面4个汉字,结果为:这个字符...
Echo "$Short_Str";
Function csubstr($str,$start,$len)
{
$strlen=strlen($str);
$clen=0;
for($i=0;$i<$strlen;$i++,$clen++)
{
if ($clen>=$start+$len)
break;
if(ord(substr($str,$i,1))>0xa0)
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,2);
$i++;
}
else
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,1);
}
} return $tmpstr;
}
Function showShort($str,$len)
{
$tempstr = csubstr($str,0,$len);
if ($str<>$tempstr)
$tempstr .= "..."; //要以什么结尾,修改这里就可以.return $tempstr;
}
?>
/**
* @file
* @version 1.0
* @author 网海浪子
* @date 2006-3-30
* @email [email protected]
* @brief php截取中英文字符串
* @转载 请注明出处
*/
?>
<?php/***********************************************
*
*截取一定长度的字符串,确保截取后字符串不出乱码
*
***********************************************/function cutstr($str,$cutleng)
{$str = $str; //要截取的字符串
$cutleng = $cutleng; //要截取的长度
$strleng = strlen($str); //字符串长度if($cutleng>$strleng)return false;//截取的长度要小于等于字符串长度 $notchinanum = 0; //初始不是汉字的字符数
for($i=0;$i<$cutleng;$i++)
{
if(ord(substr($str,$i,1))<=128)
{
$notchinanum++;
}
}
if(($cutleng%2==1)&&($notchinanum%2==0))//如果要截取奇数个字符,所要截取长度范围内的字符必须含奇数个非汉字,否则截取的长度加一
{
$cutleng++;
}
if(($cutleng%2==0)&&($notchinanum%2==1))//如果要截取偶数个字符,所要截取长度范围内的字符必须含偶数个非汉字,否则截取的长度加一
{
$cutleng++;
}
return substr($str,0,$cutleng);}
// 例程:cutstr("lirui是songxinfeng老婆",3);
?>