|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38685 ER: Allow str_replace with string subject, array replace
Submitted: 2006-09-01 22:46 UTC Modified: 2018-02-11 17:58 UTC
Avg. Score:3.7 ± 1.1
Reproduced:10 of 12 (83.3%)
Same Version:1 (10.0%)
Same OS:5 (50.0%)
From: paulschreiber at gmail dot com Assigned:
Status: Open Package: Strings related
PHP Version: 7.0 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: paulschreiber at gmail dot com
New email:
PHP Version: OS:


 [2006-09-01 22:46 UTC] paulschreiber at gmail dot com
The PHP docs for str_replace say:

"If search and replace are arrays, then str_replace() takes a value from each array and uses them to do search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search  is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though."

I think the converse makes sense in some cases. Here is an example:
$result = str_replace("?", array("Joe", "Smith"), "SELECT * FROM people WHERE first = ? AND last = ?");

(Yes, if you were doing real database work, you'd need to escape everything.)

It would be nice if PHP supported this.

Reproduce code:
$result = str_replace("?", array("'Joe'", "'Smith'"), "SELECT * FROM people WHERE first = ? AND last = ?");
print $result;

Expected result:
SELECT * FROM people WHERE first = 'Joe' AND last = 'Smith'

Actual result:
PHP Notice:  Array to string conversion in /Users/paul/- on line 2

Notice: Array to string conversion in /Users/paul/- on line 2
SELECT * FROM people WHERE first = Array AND last = Array


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-17 03:57 UTC] valentiny510 at yahoo dot es
For Paul: The syntax is incorrect.
For PHP Team: 7 years and is still open ???
 [2013-03-17 16:13 UTC]
It is a feature request, not a bug, and one that is rather iffy as far as I am 
concerned. It is likely to sit here another 7 years unless accompanied by a solid 
implementation and some better rationale on why we should encourage people to use 
something like this over the native prepare/execute syntax provided by the 
databases or even the emulated prepare/execute we provide via PDO.
 [2013-03-17 16:13 UTC]
-Status: Open +Status: Analyzed -Package: Feature/Change Request +Package: *General Issues
 [2013-03-17 16:44 UTC] paulschreiber at gmail dot com
1. When this bug was written, PDO and other similar libraries did not exist.
2. Prepared statements are only one possible use case.
3. If you are not going to fix this bug, just close it as WONTFIX.
 [2013-04-03 23:05 UTC] jeff at lxvi dot net
I agree with the OP.  There are many possible applications for cycling through an array of replacements for a single search string.  A simple example would be alternating colors for successive tr tags.  There are all kinds of unforeseen creative ways to utilize this feature, and it should not be necessary to supply you with an exhaustive list along with a doctoral dissertation.
 [2014-12-30 15:12 UTC]
-PHP Version: 4.4.4 +PHP Version: 7.0
 [2015-01-06 12:47 UTC]
-Status: Analyzed +Status: Assigned -Assigned To: +Assigned To: francois
 [2017-10-24 07:08 UTC]
-Status: Assigned +Status: Open -Assigned To: francois +Assigned To:
 [2018-02-11 17:58 UTC]
-Package: *General Issues +Package: Strings related
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jan 20 12:01:23 2020 UTC