Edit report at http://bugs.php.net/bug.php?id=52967&edit=1

 ID:                 52967
 Updated by:         cataphr...@php.net
 Reported by:        spam at geleia dot net
 Summary:            strtr array replacement difference not the same
-Status:             Open
+Status:             To be documented
 Type:               Bug
 Package:            *General Issues
 Operating System:   Win XP
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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] spam at geleia dot net

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

Reply via email to