Bug->Doc #52967 [Tbd->Opn]: strtr array replacement difference not the same

From: Date: Fri, 01 Oct 2010 23:13:11 +0000
Subject: Bug->Doc #52967 [Tbd->Opn]: strtr array replacement difference not the same
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-5154@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52967&edit=1 ID: 52967 Updated by: cataphract@php.net Reported by: wim at powerassist dot nl Summary: strtr array replacement difference not the same -Status: To be documented +Status: Open -Type: Bug +Type: Documentation Problem Package: *General Issues Operating System: Win XP PHP Version: 5.3.3 Block user comment: N Previous Comments: ------------------------------------------------------------------------ [2010-10-02 01:04:51] cataphract@php.net There seems to be no bug per se... Just bad design in packaging two different algorithms under the same function. strtr has two different implementations, depending on whether you pass it two strings or an array. In the first case, the original string is copied and then there's an in-place modification with a character-by-character transformation. For instance: var_dump(strtr("ba", "ab", "01")); gives string "10" In the other case, one appends the substitutions and the original portions into a buffer, trying the longest matches first; seems some sort of Rabin-Karp algorithm. ------------------------------------------------------------------------ [2010-10-01 22:15:10] wim at powerassist dot nl added a ) to the script. <?php $d = "Hello World"; echo strtr($d," ",""); // will print: Hello World Wrong echo strtr($d,array(" "=>"")); // will print: HelloWorld Correct ?> ------------------------------------------------------------------------ [2010-10-01 22:13:47] wim at powerassist dot nl Description: ------------ The following will not work. $d = "Hello World"; echo strtr($d," ",""); // will print: Hello World But when we do: echo strtr($d,array(" "=>""); // will print: HelloWorld Test script: --------------- <?php $d = "Hello World"; echo strtr($d," ",""); // will print: Hello World Wrong echo strtr($d,array(" "=>""); // will print: HelloWorld Correct ?> Expected result: ---------------- HelloWorld HelloWorld Actual result: -------------- Hello World HelloWorld ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52967&edit=1

« previous php.doc.bugs (#5154) next »