[PHP] php按照指定长度截取字符串的代码 →→→→→进入此内容的聊天室

来自 , 2020-11-24, 写在 PHP, 查看 142 次.
URL http://www.code666.cn/view/b3848d61
  1. <?php
  2. //if a string is longer than the defined length,  
  3. //it will add 3 periods to the end of the string.
  4. //you can change it to what ever you want for example:
  5. //return substr($str,0,$len).'[<a href="read_more.html">Read More</a>]';
  6. //http://www.sharejs.com
  7. function strLength($str,$len){
  8.     $lenght = strlen($str);
  9.     if($lenght > $len){
  10.         return substr($str,0,$len).'...';
  11.     }else{
  12.         return $str;
  13.     }
  14. }
  15. $str = "This is a fairly long string.";
  16. //The fist part is the string, the second part is how long it can be
  17. echo strLength($str,10).'<br>';
  18. echo strLength($str,30);
  19. ?>
  20. //php/8895

回复 "php按照指定长度截取字符串的代码"

这儿你可以回复上面这条便签

captcha