php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72797 Bug in str_replace with arrays if the second one starts with a space
Submitted: 2016-08-09 18:31 UTC Modified: 2016-08-09 19:07 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: info at maduranma dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.0.9 OS: Windows
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: info at maduranma dot com
New email:
PHP Version: OS:

 

 [2016-08-09 18:31 UTC] info at maduranma dot com
Description:
------------
In the code below, if you change the first "' '" of the second array in the str_replace function with some kind of value it doesn't happen, but if you do not, it takes it to the last value of the array, that's "'0'" and you can change it to "'ssdsad'" (for example) and you'll see that it takes the last value.

Test script:
---------------
$msg = 'AABB';
$msg = str_replace(array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'Ñ', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' '), array(' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'Ñ', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'), $msg); // N-1
echo $msg;

Expected result:
----------------
  AA

Actual result:
--------------
00AA

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-09 18:39 UTC] info at maduranma dot com
To make it easiest:
$msg = 'AA  ';
$msg = str_replace(array('A', ' '), array(' ', 'A'), $msg);
echo $msg;
It gives 'AAAA' instrad of '  AA', and, if you change 'A' like this:
$msg = 'AA  ';
$msg = str_replace(array('A', ' '), array(' ', 'A2'), $msg);
echo $msg;
It returns 'A2A2A2A2'
 [2016-08-09 19:04 UTC] info at maduranma dot com
IMPORTANT EDIT:
It not only happens with spaces, it happens with the first value too, it does the replacement 2 times if it's the first and just the first, like you can see here:
str_replace(array('A', 'B', 'C', 'O'), array('O2', 'A', 'B', 'C'), 'AABB');
 [2016-08-09 19:07 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2016-08-09 19:07 UTC] nikic@php.net
str_replace() handles all replacement pairs in sequence. in (A, B)=>(B, A) it will first replace A with B and then B with A. See the big "Caution" note on http://php.net/manual/en/function.str-replace.php.

If this is not the behavior you want, use the strtr() function instead, which will perform all replacements simultaneously.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC