|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-09-28 09:54 UTC] 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!
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 19:00:02 2025 UTC |
> 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)