php按照指定长度截取字符串的代码
2018-07-20 来源:open-open
php按照指定长度截取字符串的代码,如果字符串超出了指定的长度,会用...替换,不过这段代码不支持中英文的区分
<?php
//if a string is longer than the defined length,
//it will add 3 periods to the end of the string.
//you can change it to what ever you want for example:
//return substr($str,0,$len).'[<a href="read_more.html">Read More</a>]';
//http://www.sharejs.com
function strLength($str,$len){
$lenght = strlen($str);
if($lenght > $len){
return substr($str,0,$len).'...';
}else{
return $str;
}
}
$str = "This is a fairly long string.";
//The fist part is the string, the second part is how long it can be
echo strLength($str,10).'<br>';
echo strLength($str,30);
?>
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:php计算密码强度
最新资讯
热门推荐