Posted By

mindshare on 03/11/11


Tagged

php String limit word limiter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tmmyy


Super-short PHP Word Limiter


 / Published in: PHP
 

Cut PHP strings at full word breaks.

  1. implode(' ',array_slice(explode(' ', $string),0,$length));
  2.  
  3. /* usage:
  4. $string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
  5. $length = 3;
  6. echo implode(' ',array_slice(explode(' ', $string),0,$length));
  7. */

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: vitruvo on August 24, 2011

If you´re having trouble with Special Chars try using "explode" to seperate the words: implode(' ',array_slice(explode(' ',$string),0,$length));

Posted By: mindshare on February 24, 2012

Thanks vitruvo! I actually discovered that issue too - I'm updating the code.

You need to login to post a comment.