|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-10-01 22:13 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 13:00:02 2025 UTC |
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 ?>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.