php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #40863 str_ireplace not behaving as expected
Submitted: 2007-03-20 10:10 UTC Modified: 2007-03-23 14:11 UTC
From: jonathan at sadowski dot us Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.1 OS: Ubuntu
Private report: No CVE-ID: None
 [2007-03-20 10:10 UTC] jonathan at sadowski dot us
Description:
------------
str_ireplace doesn't behave as expected when $search and $replace are equally sized arrays, and $subject is a string containing mixed case.

Reproduce code:
---------------
<?php
$phrase = "We the People of the United States";
$letters = explode(" ","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");
$key = $letters;
shuffle($key);
echo sizeof($letters) . "\n" . sizeof($key) . "\n\n";
echo implode(" ",$letters) . "\n" . implode(" ",$key) . "\n" . str_ireplace($letters,$key,$phrase);
?>


Expected result:
----------------
26
26

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
V T U O W Q E X Y K S D Z B M A I L J R C F G H P N
GW RXW AWMADW MQ RXW CBYRWO JRVRWJ

Actual result:
--------------
26
26

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
V T U O W Q E X Y K S D Z B M A I L J R C F G H P N
GG RHG AGMADG MI RHG CBPRGM JRFRGJ

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-20 10:20 UTC] tony2001@php.net
..E .. W 
..W .. G 

E replaced with W
W replaced with G.
 [2007-03-20 10:28 UTC] jonathan at sadowski dot us
Perhaps you should read the expected result before marking it bogus, and read through the sample code to see what its supposed to do.

The first word is "We", this cannot produce "GG" no matter what, as "W" and "e" are two different words.
 [2007-03-20 10:40 UTC] jonathan at sadowski dot us
Here's a more concrete example...  The last line of the output should be "WE THE PEOPLE OF THE UNITED STATES"

<?php
$phrase = "We the People of the United States";
$letters = explode(" ","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");
$key = explode(" ","V T U O W Q E X Y K S D Z B M A I L J R C F G H P N");
$TMP1 = str_ireplace($letters,$key,$phrase);
$TMP2 = str_ireplace($key,$letters,$TMP1);
echo $phrase . "\n";
echo $TMP1 . "\n";
echo $TMP2 . "\n";
?>


Actual output:
We the People of the United States
GG RHG AGMADG MI RHG CBPRGM JRFRGJ
WW TXW YWOYTW OQ TXW UZYTWO STVTWS
 [2007-03-20 10:41 UTC] tony2001@php.net
Please read what I say:
str_ireplace() changes 'E' and 'e' to 'W' and then changes 'W' to 'G'.
 [2007-03-20 10:49 UTC] jonathan at sadowski dot us
Then at the very least it's a documentation problem, because that is not clear in the documentation... to me, that's not desired functionality.
 [2007-03-23 13:48 UTC] vrana@php.net
"Every replacement with search array is performed on the result of previous replacement."
 [2007-03-23 14:11 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 23:01:33 2025 UTC