php中一些有用的函数[不断增加中]
时间:2007-02-22 来源:elecjack
从今天起开始记录一些常用的php函数
2007-3-4
round($var1,$var2)
根据指定的精度对给定的数进行四舍五入。
round(3.4);//3
round(3.5);//4
round(1.9567,2);//1.96
2007-2-22
获取字符串的长度:
strlen($string)
下面我再给出一段用于截取中文字符的代码,这段代码对于排放新闻列表相当的有用,对于页面的美观可以起到不小的帮助作用:
function cn_midstr($str,$start,$len){
$i=0;
$dd=0;
while($i$start)
{
$ch=substr($str,$i,1);
if(ord($ch)>127) $dd++;
else $dd=$dd+2;
$i++;
}
if($dd%2!=0) $start++;
$i=$start;
$endnum = $start+$len;
while($i$endnum)
{
$ch=substr($str,$i,1);
if(ord($ch)>127) $i++;
$i++;
}
$restr=substr($str,$start,$i-$start);
return $restr;
}
以上这段代码已经封装成为一个函数,可以直接拿过来用,因为中文字符每个字占两个字节,为了能接到完整的中文字符,用这个函数最好不过了。譬如一条新闻标题$string有50个字节(25个汉字),但是我只想显示前面的15个汉字,则用上面的函数就可以做到cn_midstr($string,0,30)。
相关阅读 更多 +
排行榜 更多 +