php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #11457 limit for str_replace()
Submitted: 2001-06-13 01:51 UTC Modified: 2010-12-01 16:24 UTC
Votes:50
Avg. Score:4.6 ± 0.7
Reproduced:40 of 42 (95.2%)
Same Version:14 (35.0%)
Same OS:14 (35.0%)
From: adamw at uscho dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.0.5 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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: adamw at uscho dot com
New email:
PHP Version: OS:

 

 [2001-06-13 01:51 UTC] adamw at uscho dot com
I've been racking my brain attempting to figure out a way to replace only the first instance of a specified string within a larger string.

Obviously - str_replace does this ... but it does it for *all* instances.  Other functions will *find* the first instance, but won't act upon it.

I noticed, however, that "preg_replace" has a fourth parameter ... the "limit" parameter.

How nice would this be for "str_replace" to have that 4th parameter?!  It would avoid the need to use a regular expression function.

I am open to workarounds and alternatives.  Thank you for the consideration.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-31 09:21 UTC] andrey@php.net
As of 5.0.0 the last parameter (count) is passed by reference (it is optional). So 2 choices :
1)passed not by reference - it's limit
2)passed by reference : the var is is_null() - all occurences are replaced the number is returned in "count", otoh if the var !is_null() -> use as limit.

Comments?
 [2003-06-06 10:19 UTC] juwe at clasennet dot de
Why not simply add a further optional argument? This way You could even distinguish if less values where replaced.

Furthermore I'd like "limit" optionally to be an array, if "search" is of that type. This way You could specify different  replace limitations for differrent strings to be replaced.

Please correct me if I'm misled, but as far as I've seen in the sourcecode str_replace() is internally subdivided into two functions anyway, one "interface" and another function doing the "real work" for str_replace() and str_ireplace(). (BTW: Why not stri_replace()? Seems to be more apropriate..)
By implementing this feature we could easily have the same for str_ireplace() as well without much work and therfor avoid lots of usage of regular expressions.
 [2007-10-17 14:39 UTC] jetweedy at hotmail dot com
In response to this, here's a simple function to replace the first one only (note that you could loop this function to replace more than one, but not all):

function str_replaceFirst($s,$r,$str)
{
	$l = strlen($str);
	$a = strpos($str,$s);
	$b = $a + strlen($s);
	$temp = substr($str,0,$a) . $r . substr($str,$b,($l-$b));
	return $temp;
}
 [2009-04-19 01:46 UTC] phpnet at sabintr dot com
How about this...

$count is passed by reference. Don't change this.
if (is_null($count)) {
   //replace all;
} elseif (floor($count) < 1) {
   //replace all;
} else {
   //replace first floor($count) occurences;
}
$count = number of actual replacements made

The only change from past behavior is that $count must be unset or made NULL before passing it by reference:

echo str_replace("o", "a", "moo", $count); // returns "maa"
echo str_replace("o", "a", "mooooo", $count); // returns "maaooo"
unset($count);
echo str_replace("o", "a", "mooooo", $count); // returns "maaaaa"

Thoughts?
 [2009-11-18 16:44 UTC] felipe dot tonello at felipetonello dot com
On version 5.3.0 the 4th parameter, count, does not work when not passed 
by reference.

$count = 1;
echo str_replace("o", "a", "foo", $count); // returns "faa"

Even if it was working well, thought, this is not a good approach.
 [2010-12-01 16:24 UTC] jani@php.net
-Status: Assigned +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues -Assigned To: pollita +Assigned To:
 [2010-12-01 16:24 UTC] jani@php.net
Use preg_replace() if you need this.
 [2011-01-17 02:59 UTC] TrentTompkins at gmail dot com
This is such a great idea! I can't believe it was suggested 10 years ago and no one added it =/
 [2011-04-27 08:30 UTC] cancausecancerr at yahoo dot com dot cn
I would vote to add a str_replaceonce function where the $count arg specifies which occurrence in the string to change (negatives also.)

Changing the existing str_replace is something people can do on their own installation but I don't think it will be changed in the main code because it could break some existing software written in php.
 [2012-07-09 17:55 UTC] bfrohs at gmail dot com
Here's a workaround written in PHP that is 100% backward-compatible with str_replace: http://stackoverflow.com/a/11400172/526741
 [2014-04-05 11:06 UTC] ullisses at gmail dot com
Are you kidding me? 13 years and "Won't fix"?

Simple code:
$bb_code = array('[b]','[/b]','[i]','[/i]','[u]','[/u]');
$html_code = array('<b>','</b>','<i>','</i>','<u>','</u>');
$html = str_replace($bb_code,$html_code,$edit);

Task: 
Allow to use bbcode only 2 times in whole article.

Good luck with preg_replace. 
I know it's possible, but when it comes to performance... yeah, right.

I'm too 'oldschool' to use "plugins" and "libraries" to replace BBCode, and much too old to waste my servertime for preg_replace.

After years of crappy programming and wasting resources it's time to start doing it right - as it was years ago.
So for God sake just add this simple limit, as you count replaces anyway...
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC