Your Ad Here

Posted By

StevenW721 on 07/09/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

baires
joaofgf


Trim text to X number of words


 / Published in: PHP
 

  1. function trimText ( $text, $trimNumber )
  2. { // trim the text to trimNumber amount of words
  3. $text = str_replace(" ", " ", $text);
  4. $trimmed = NULL;
  5. $string = explode(" ", $text);
  6. if ( count($string) > $trimNumber )
  7. { for ( $wordCounter = 0 ; $wordCounter <= $trimNumber ; $wordCounter++ ){
  8. $trimmed .= $string[$wordCounter];
  9. if ( $wordCounter < $trimNumber ){ $trimmed .= " "; }
  10. else { $trimmed .= "..."; }
  11. }
  12. }
  13. else
  14. { $trimmed = $text; }
  15. $trimmed = stripslashes(trim($trimmed));
  16. return ($trimmed);
  17. }

Report this snippet  

You need to login to post a comment.