php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35232 error in str_replace/str_ireplace with array type arguments
Submitted: 2005-11-15 23:55 UTC Modified: 2005-11-16 00:15 UTC
From: o_eclipse at mail dot ru Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.0.5 OS: Windows
Private report: No CVE-ID: None
 [2005-11-15 23:55 UTC] o_eclipse at mail dot ru
Description:
------------
Functions str_replace / str_ireplace give incorrect result when 'replace' argument is array filled with numbers.

Reproduce code:
---------------
<?php
$subject  = "*0-1-2-3-4-5-6-7-8-9*";
$search   = array ( "-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" );
$replace  = array ( "+", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" );
$result   = str_replace($search, $replace, $subject);
echo "$result\n"; // result is *a+b+c+d+e+f+g+h+i+j*
$replace  = array ( "+", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0" );
$result   = str_replace($search, $replace, $subject);
echo "$result\n"; // result is *0+1+2+3+4+4+3+2+1+0* 
                  // must be   *0+1+2+3+4+5+6+7+8+9*
?>

Expected result:
----------------
the program output should be like this:
*a+b+c+d+e+f+g+h+i+j*
*0+1+2+3+4+5+6+7+8+9*

Actual result:
--------------
that`s what we get:
*a+b+c+d+e+f+g+h+i+j*
*0+1+2+3+4+4+3+2+1+0*

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-16 00:15 UTC] tony2001@php.net
You're replacing 1 with 8 and *after that* 8 with 1.
Same goes with 6,7 & 9.
Obviously you're wrong with your expectations.
No bug here. 
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Fri Apr 03 23:00:01 2026 UTC