Bug #72797 [Opn]: Bug in str_replace with arrays if the second one starts with a space
| From: | info at maduranma dot com | Date: | Tue, 09 Aug 2016 18:39:11 +0000 |
| Subject: | Bug #72797 [Opn]: Bug in str_replace with arrays if the second one starts with a space | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-203123@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72797&edit=1
ID: 72797
User updated by: info at maduranma dot com
Reported by: info at maduranma dot com
Summary: Bug in str_replace with arrays if the second one
starts with a space
Status: Open
Type: Bug
Package: Arrays related
Operating System: Windows
PHP Version: 7.0.9
Block user comment: N
Private report: N
New Comment:
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'
Previous Comments:
------------------------------------------------------------------------
[2016-08-09 18:31:53] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72797&edit=1