php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24281 str_replace count functionality
Submitted: 2003-06-21 17:46 UTC Modified: 2003-06-23 09:34 UTC
From: jimflug2222 at hotmail dot com Assigned:
Status: Closed Package: Strings related
PHP Version: 5CVS-2003-06-21 (dev) OS: GNU/Linux
Private report: No CVE-ID: None
 [2003-06-21 17:46 UTC] jimflug2222 at hotmail dot com
Description:
------------
I tried out the new fourth parameter in str_replace to count the 
number of replacements made (this was using the very latest 
snapshot), and found that the fourth parameter only works if the 
variable passed is set already. 

Reproduce code:
---------------
Interactive mode enabled

$string = "He had had to have had it";
$newstring = str_replace("had", "foo", $string, $count);
print "$count changes were made.\n";
 changes were made.
$count = 0;
$newstring = str_replace("had", "foo", $string, $count);
echo $count;
3

(Note: due to ambiguity in the manual, I tried using $count and &$count as I wasn't sure which was correct - both failed to work)

Expected result:
----------------
I would have expected either a warning, "Unknown variable 
$count", or, otherwise, for PHP to create $count and set it to the 
right value. 

Actual result:
--------------
$count didn't get set 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-23 09:34 UTC] stas@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Feb 24 16:01:27 2024 UTC