Req #62315 [Opn->Csd]: str_replace() to accept search as a string while replace is an array

From: Date: Fri, 09 Jan 2015 12:05:37 +0000
Subject: Req #62315 [Opn->Csd]: str_replace() to accept search as a string while replace is an array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189834@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62315&edit=1

 ID:                 62315
 User updated by:    narf at devilix dot net
 Reported by:        narf at devilix dot net
 Summary:            str_replace() to accept search as a string while
                     replace is an array
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            Unknown/Other Function
 Operating System:   Irrelevant
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Duplicate of #38685 / https://bugs.php.net/bug.php?id=38685


Previous Comments:
------------------------------------------------------------------------
[2012-06-13 16:28:19] narf at devilix dot net

Description:
------------
---
From manual page: http://www.php.net/function.str-replace#refsect1-function.str-replace-parameters
---

> If search and replace are arrays, then str_replace() takes a value from each array and uses
> them to 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.

That last sentence is what bothers me, as I have a use case where it DOES make sense. In the patch
below (line 646), I assumed that this is an accepted behavior:

https://github.com/EllisLab/CodeIgniter/commit/10cbdf091b3cdbc72847dad28a1dce03a92119b6

To shortly explain what the patched method does - it's basically a pseudo prepared statement
compiler. It takes an SQL query and an array of values and then replaces each occurence of
'?' (default value for $this->bind_marker) with the corresponding (by position) element
from the input array.

Test script:
---------------
<?php
$query = 'UPDATE table_name SET field1_name = :? WHERE
field2_name = :?';
$bind_marker = '?';
$bind_values = array("'foo'", "'bar'");
echo str_replace($bind_marker, $bind_values, $sql);

Expected result:
----------------
UPDATE table_name SET field1_name = 'foo' WHERE
field2_name = 'bar'

Actual result:
--------------
PHP Notice:  Array to string conversion in php shell code on line 5
UPDATE table_name SET field1_name = Array WHERE field2_name =
Array



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=62315&edit=1


Thread (2 messages)

« previous php.bugs (#189834) next »