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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Wed May 08 10:01:32 2024 UTC