Req #11457 [Asn->Wfx]: limit for str_replace()
| From: | jani@php.net | Date: | Wed, 01 Dec 2010 15:24:24 +0000 |
| Subject: | Req #11457 [Asn->Wfx]: limit for str_replace() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-156648@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=11457&edit=1
ID: 11457
Updated by: jani@php.net
Reported by: adamw at uscho dot com
Summary: limit for str_replace()
-Status: Assigned
+Status: Wont fix
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
PHP Version: 4.0.5
-Assigned To: pollita
+Assigned To:
Block user comment: N
Private report: N
New Comment:
Use preg_replace() if you need this.
Previous Comments:
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
[2003-05-31 09:21:10] 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?
------------------------------------------------------------------------
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