Bug #72797 [Opn->Nab]: Bug in str_replace with arrays if the second one starts with a space

From: Date: Tue, 09 Aug 2016 19:07:18 +0000
Subject: Bug #72797 [Opn->Nab]: 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-203125@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 Updated by: nikic@php.net Reported by: info at maduranma dot com Summary: Bug in str_replace with arrays if the second one starts with a space -Status: Open +Status: Not a bug Type: Bug Package: Arrays related Operating System: Windows PHP Version: 7.0.9 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2016-08-09 19:04:18] 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 18:39:10] 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 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

« previous php.bugs (#203125) next »