php.net |  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
Votes:1
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: frank at vista dot com
New email:
PHP Version: OS:

 

 [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. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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