fork download
  1. <?php
  2. $string = "the quick brown fox jumps over the lazy dog";
  3. for($limit = 10; $limit <= strlen($string); $limit += 10) {
  4. print_r(display_short($string, $limit));
  5. }
  6.  
  7. function display_short($str, $limit, $ellipsis = '...') {
  8. if (strlen($str) <= $limit) {
  9. return $str;
  10. }
  11.  
  12. $limit -= strlen($ellipsis);
  13. $pos = strrpos($str, ' ', $limit - strlen($str));
  14. if ($pos === false) {
  15. $pos = $limit;
  16. }
  17.  
  18. return substr($str, 0, $pos).$ellipsis;
  19. }
  20.  
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
the...the quick brown...the quick brown fox jumps...the quick brown fox jumps over the...