Blogger Information
Blog 16
fans 0
comment 1
visits 13094
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
strpos和stripos函数 字符串截取
IT浪子走天涯
Original
1174 people have browsed it

这是两个重要的函数

strpos(全部字符串,需要检索的字符串);需要检索的字符串区分大小写

stripos(全部字符串,需要检索的字符串);需要检索的字符串不区分大小写

//字符串截取

function cut_str($sourcestr,$cutlength)  {

$returnstr='';

$i=0;

$n=0;

$str_length=strlen($sourcestr);//字符串的字节数  

while (($n<$cutlength) and ($i<=$str_length))

{

$temp_str=substr($sourcestr,$i,1);

$ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码  

if ($ascnum>=224)    //如果ASCII位高与224,  

{

$returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符          

$i=$i+3;            //实际Byte计为3  

$n++;            //字串长度计1  

}

elseif ($ascnum>=192) //如果ASCII位高与192,  

{

$returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符  

$i=$i+2;            //实际Byte计为2  

$n++;            //字串长度计1  

}

elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母,  

{

$returnstr=$returnstr.substr($sourcestr,$i,1);

$i=$i+1;            //实际的Byte数仍计1个  

$n++;            //但考虑整体美观,大写字母计成一个高位字符  

}

else                //其他情况下,包括小写字母和半角标点符号,  

{

$returnstr=$returnstr.substr($sourcestr,$i,1);

$i=$i+1;            //实际的Byte数计1个  

$n=$n+0.5;        //小写字母和半角标点等与半个高位字符宽...  

}

}

if ($str_length>$i){

$returnstr = $returnstr . "......";//超过长度时在尾处加上省略号  

}

return $returnstr;


}

然后在tp5中调用

{$brand.brand_description | cut_str=###,15}

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post