language: PHP (php 5.4.4)
date: 388 days 20 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$string = "the quick brown fox jumps over the lazy dog";
for($limit = 10; $limit <= strlen($string); $limit += 10) {
    print_r(display_short($string, $limit));
}
 
function display_short($str, $limit, $ellipsis = '...') {
    if (strlen($str) <= $limit) {
        return $str;
    }
 
    $limit -= strlen($ellipsis);
    $pos = strrpos($str, ' ', $limit - strlen($str));
    if ($pos === false) {
        $pos = $limit;
    }
    
    return substr($str, 0, $pos).$ellipsis;
}