|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49007 Add limit option to trim functions
Submitted: 2009-07-21 17:06 UTC Modified: 2021-08-22 04:22 UTC
Avg. Score:4.2 ± 1.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: jason at jasonsw dot com Assigned: cmb (profile)
Status: No Feedback Package: Strings related
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: jason at jasonsw dot com
New email:
PHP Version: OS:


 [2009-07-21 17:06 UTC] jason at jasonsw dot com
It would be great if there was a way to limit the amount of characters that were trimmed from a string, in either ltrim, trim or rtrim, using a new limit parameter.

Reproduce code:
From manual page: function.ltrim


feature-49007.diff (last revision 2011-02-27 10:10 UTC by jthijssen at noxlogic dot nl)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 16:19 UTC]
-Summary: add a third parameter to ltrim: limit +Summary: Add limit option to trim functions -Package: Feature/Change Request +Package: Strings related -Operating System: Debian/Linux +Operating System: * -PHP Version: 5.3.0 +PHP Version: *
 [2011-02-26 21:58 UTC] jthijssen at noxlogic dot nl
I've added a patch (both 5.3 and trunk) for adding a "limit" to the [lr]?trim() functions. 

Using a limit of -1 will trim all (same as without adding the limit)
A limit of 0 does nothing (no trimming)
A positive limit will only trim the amount of chars as specified ON BOTH SIDES. This means using a limit of 2 in trim() will strip max 2 chars from the left and max 2 chars from the right:

trim("   hello   ", " ", 2)  returns " hello ";
ltrim("   hello   ", " ", 2) returns " hello   ";

The only problem to keep it backwards compatible is that you must add the default charlist ( \n\r\t\v\0) in order to use the limit parameter.
 [2016-12-30 14:33 UTC]
I wonder what would be a use-case for the $limit parameter.
 [2021-08-09 15:38 UTC]
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-08-09 15:38 UTC]
> I wonder what would be a use-case for the $limit parameter.

And I still do.  Can someone please clarify?
 [2021-08-22 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Feb 21 06:01:29 2024 UTC