From:             [EMAIL PROTECTED]
Operating system: Linux (Redhat 7.2)
PHP version:      4.1.2
PHP Bug Type:     Feature/Change Request
Bug description:  substr with pretty output

Often times you want to display the name of something in a list (say, items
in a store) but have limited space. 

You can simply truncate strings that are longer than x characters and add
an ellipsis, but occasionally you end up with:

"The quick brown fox ..." or "The quick brown f...". When "The quick brown
fox..." or "The quick brown..." (respectively) would be nicer. 

In PHP Code:

function pretty_substr($string, $length, $slop = 4) {
  if (strlen($string) > $length) {
    $string = substr($string, 0, $length - 1);

    $lastSpace = strrpos($string, ' ');

    if ($lastSpace > $length - $slop) {
      $string = substr($string, 0, $lastSpace);
    }
        
    $string .= '…';
  }

  return $string;
}

I think this would make a good addition to the string function library. 
-- 
Edit bug report at http://bugs.php.net/?id=17057&edit=1
-- 
Fixed in CVS:        http://bugs.php.net/fix.php?id=17057&r=fixedcvs
Fixed in release:    http://bugs.php.net/fix.php?id=17057&r=alreadyfixed
Need backtrace:      http://bugs.php.net/fix.php?id=17057&r=needtrace
Try newer version:   http://bugs.php.net/fix.php?id=17057&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=17057&r=support
Expected behavior:   http://bugs.php.net/fix.php?id=17057&r=notwrong
Not enough info:     http://bugs.php.net/fix.php?id=17057&r=notenoughinfo
Submitted twice:     http://bugs.php.net/fix.php?id=17057&r=submittedtwice
register_globals:    http://bugs.php.net/fix.php?id=17057&r=globals

Reply via email to