Edit report at http://bugs.php.net/bug.php?id=11457&edit=1

 ID:                 11457
 Comment by:         TrentTompkins at gmail dot com
 Reported by:        adamw at uscho dot com
 Summary:            limit for str_replace()
 Status:             Wont fix
 Type:               Feature/Change Request
 Package:            *General Issues
 PHP Version:        4.0.5
 Block user comment: N
 Private report:     N

 New Comment:

This is such a great idea! I can't believe it was suggested 10 years ago
and no one added it =/


Previous Comments:
------------------------------------------------------------------------
[2010-12-01 16:24:23] j...@php.net

Use preg_replace() if you need this.

------------------------------------------------------------------------
[2009-11-18 16:44:37] 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.

------------------------------------------------------------------------
[2009-04-19 01:46:26] 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?

------------------------------------------------------------------------
[2007-10-17 14:39:22] 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;

}

------------------------------------------------------------------------
[2003-06-06 10:19:08] 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.

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    http://bugs.php.net/bug.php?id=11457


-- 
Edit this bug report at http://bugs.php.net/bug.php?id=11457&edit=1

Reply via email to