|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #17057 substr with pretty output
Submitted: 2002-05-06 21:04 UTC Modified: 2010-12-01 16:11 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: frank at vista dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.1.2 OS: Linux (Redhat 7.2)
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2002-05-06 21:04 UTC] frank at vista dot com
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. 


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-07 01:44 UTC]
Since there was talk someone (Daniel??) will work in this for PEAR, I reclassify this.
 [2002-05-30 04:07 UTC]
Once there will be a Text/Word package, we will possibly realize something like this. => Analyzed.
 [2002-10-20 15:55 UTC]
This is not a bug, rather a feature request.
 [2010-12-01 16:11 UTC]
-Status: Analyzed +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2010-12-01 16:11 UTC]
This is something you implement yourself.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Oct 22 19:01:26 2018 UTC