PHP Limit Words


/ Published in: PHP
Save to your folder(s)

Example:

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

Will return "This is"


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.