Bug #76944 [Opn->Nab]: 4th parameter to str_replace / str_ireplace does not work

From: Date: Fri, 28 Sep 2018 10:12:24 +0000
Subject: Bug #76944 [Opn->Nab]: 4th parameter to str_replace / str_ireplace does not work
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217259@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76944&edit=1 ID: 76944 Updated by: salathe@php.net Reported by: een dot meel dot of dot geen dot meel at gmail dot com Summary: 4th parameter to str_replace / str_ireplace does not work -Status: Open +Status: Not a bug Type: Bug Package: *General Issues Operating System: any PHP Version: 7.0.32 Block user comment: N Private report: N New Comment: > str_replace and str_ireplace have a 4th argument that should allow one to limit the amount of > replacements. This is not the case. The $count argument should be a variable, which will be populated with the number of replacements that happened. Perhaps you confused it with the $limit argument for functions like preg_replace() and friends. The following shows that the "a" in "banana" was replaced 3 times: $counter = 0; $result = str_replace("a", "", "banana", $counter); var_dump($result, $counter); // string(3) "bnn" // int(3) Previous Comments: ------------------------------------------------------------------------ [2018-09-28 09:54:08] een dot meel dot of dot geen dot meel at gmail dot com Description: ------------ str_replace and str_ireplace have a 4th argument that should allow one to limit the amount of replacements. However, this 4th replacement does not work. Furthermore, the 4th replacement MUST be a variable that is passed by reference for some reason?? Test script: --------------- <?php error_reporting(1); echo str_replace('Hel', '', 'HelHello people! Helloooo! helloo!', 1)."\n"; echo str_ireplace('hel', '', 'HelHello people!!! Helloooo! helloo!', 1)."\n"; $ct = 1; echo str_replace('Hel', '', 'HelHello people! Helloooo! helloo!', $ct)."\n"; echo str_ireplace('hel', '', 'HelHello people!!! Helloooo! helloo!', $ct)."\n"; Expected result: ---------------- Hello people! Helloooo! helloo! Hello people! Helloooo! helloo! Hello people! Helloooo! helloo! Hello people! Helloooo! helloo! Actual result: -------------- (Multiple runs, obviously) > PHP Fatal error: Only variables can be passed by reference in /opt/local/apache2/htdocs/x.php on line 4 > PHP Fatal error: Only variables can be passed by reference in /opt/local/apache2/htdocs/x.php on line 5 > lo people! loooo! helloo! lo people!!! loooo! loo! ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76944&edit=1

« previous php.bugs (#217259) next »