这是一个经常用到的功能,如果字符串太长,需要按照指定的长度进行截断,然后在后面加上三个点的省略号
<?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>]'; 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); ?>