php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37099 str_replace with arrays - weird behaviour
Submitted: 2006-04-16 18:35 UTC Modified: 2006-04-16 19:03 UTC
From: admin at ifyouwantblood dot de Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.1.2 OS: Linux AND windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: admin at ifyouwantblood dot de
New email:
PHP Version: OS:

 

 [2006-04-16 18:35 UTC] admin at ifyouwantblood dot de
Description:
------------
str_replace does not replace all array values with the associated array replace.

Reproduce code:
---------------
<?php

$string = "test";

test($string);

function test($datei)
{
	$search_s=range('a','z');
	$replace_s=range('z','a');

   	$code = str_replace($search_s, $replace_s, $datei);
		echo $code."\n";
	$decode = str_replace($replace_s, $search_s, $code);
		echo $decode;
} 

?>

Expected result:
----------------
gvhg
test

Actual result:
--------------
gehg 
tvst

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-16 19:03 UTC] tony2001@php.net
The problem is that both of your arrays contain the same characters. So the first str_replace() changes 'e' to 't' and then changes it back when it reaches 't' in the $replace_s array.
No bug here, just a user error.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 16:02:26 2025 UTC