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

From: Date: Sat, 02 Oct 2010 08:48:43 +0000
Subject: Doc #52967 [Opn->Csd]: strtr array replacement difference not the same
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-5155@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: Open +Status: Closed Type: Documentation Problem Package: *General Issues Operating System: Win XP PHP Version: 5.3.3 -Assigned To: +Assigned To: cataphract Block user comment: N New Comment: This bug has been fixed in SVN. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. 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 (#5155) next »