Posted By

MichaelM on 10/25/11


Tagged

words limit


Versions (?)

PHP Limit Words


 / Published in: PHP
 

Example:

echo limitWords('This is my string personally i think its too long',2);

Will return "This is"

  1. function limitWords($str,$limit){
  2. $wc = explode(' ',$str);
  3. $c = count($wc);
  4. $r = '';
  5. if($c <= $limit)
  6. $r = $str;
  7. else{
  8. $i=0;
  9. while($i<$limit){
  10. $r .= $wc[$i].' ';
  11. $i++;
  12. }
  13. }
  14. return $r;
  15. }

Report this snippet  

You need to login to post a comment.