Shorten text without braking words


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



Copy this code and paste it in your HTML
  1. /*
  2. returns text limited by a specified length of characters but keeping words intact. the final character count will not be exact since it is affected by the possible removing of the a long word or by the addition of the ellipsis.
  3.  
  4. paramaters:
  5. string - the input string
  6. chars - the length of characters wanted
  7. elli - the ellipsis to be used, defaults to '...'
  8. */
  9.  
  10. function shorten($string='', $chars=20, $elli='...'){
  11. list($new_string, $elli)= explode("\n", wordwrap($string, $chars, "\n", false));
  12. return ( $elli ) ? $new_string.'...' : $new_string;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.