php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75902 str_replace should warn when misused with nested arrays
Submitted: 2018-02-01 10:19 UTC Modified: 2020-03-09 10:55 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: anonymous at yopmail dot com Assigned: nikic (profile)
Status: Closed Package: Strings related
PHP Version: Next Minor Version OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
14 + 34 = ?
Subscribe to this entry?

 
 [2018-02-01 10:19 UTC] anonymous at yopmail dot com
Description:
------------
str_replace works well with a string <https://3v4l.org/oH6HQ>
or with a single-level (one-dimensional) array <https://3v4l.org/ED1hB>
but I have seen it misused with a multi-dimensional array <https://3v4l.org/tWEWt>
 in which case it does not replace anything (it is not recursive) but does not trigger any warning either (the change of behaviour between 4.3.1 and 4.3.2 comes from Bug #23654).
I would like it to at least inform us that it is being misused.
Thanks

Test script:
---------------
<?php

$a = array (
  0 => 
  array (
    'one' => 'A {bad} thing.',
    'two' => 'So far so {bad}.',
  ),
  1 => 
  array (
    'one' => 'A {bad} thing, bis.',
    'two' => 'So far so {bad}, bis.',
  ),
);

$b = str_replace('{bad}', 'good', $a);

echo '$b = ';
var_export($b);
echo ';', "\n";


Expected result:
----------------
Some notice about str_replace being misused (while keeping the result untouched).

Actual result:
--------------
No notice at all. $b is identical to $a (untouched).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-09 10:39 UTC] anonymous at yopmail dot com
Would some dev please care to comment? It is even more inconsistent as preg_replace wasn't affected by the "fix" of Bug #23654 (see <https://3v4l.org/11lUa>)...
 [2020-03-09 10:55 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2020-03-10 15:52 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e8678fcb42c5cb1ea38ff9c6819baca74c2bb5ea
Log: Fixed bug #75902
 [2020-03-10 15:52 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 17:01:31 2024 UTC