Your Ad Here

Posted By

liamchapman on 11/04/10


Tagged


Versions (?)

String Trimmer


 / Published in: PHP
 

Updated trimmer, there were a couple of typos.

  1. <?php
  2.  
  3. function trimmer($str, $length=100, $link=null, $tail='...', $wrap="<p>", $tags=false)
  4. {
  5.  
  6. //start wrap if set
  7. $open = !is_null($wrap) && $tags == false ? $wrap : '';
  8.  
  9. //check length and add tail if needed.
  10. if(strlen($str) > $length)
  11. {
  12.  
  13. $str = substr($str, 0 , $length);
  14. $str .= $tail;
  15.  
  16. }//end if
  17.  
  18. //check if we want tags or not
  19. $str = $tags == false ? strip_tags($str) : $str;
  20.  
  21. //add link if set
  22. $str .= !is_null($link) ? '&nbsp;<a href="'.$link.'">Read more</a>'."
  23. " : '';
  24.  
  25. //end wrap if set
  26. $close = !is_null($wrap) && $tags == false ? '</'. substr($wrap, 1) : '';
  27.  
  28. return $open.$str.$close;
  29.  
  30. }//end of trimmer
  31.  
  32. ?>

Report this snippet  

You need to login to post a comment.