php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52967 strtr array replacement difference not the same
Submitted: 2010-10-01 22:13 UTC Modified: 2010-10-02 10:50 UTC
From: wim at powerassist dot nl Assigned: cataphract (profile)
Status: Closed Package: *General Issues
PHP Version: 5.3.3 OS: Win XP
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wim at powerassist dot nl
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-01 22:15 UTC] 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-02 01:04 UTC] cataphract@php.net
-: wim@powerassist.nl +: spam at geleia dot net -Status: Open +Status: To be documented
 [2010-10-02 01:04 UTC] 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-02 01:05 UTC] cataphract@php.net
-: spam@geleia.net +: wim at powerassist dot nl
 [2010-10-02 01:13 UTC] cataphract@php.net
-Status: To be documented +Status: Open -Type: Bug +Type: Documentation Problem
 [2010-10-02 10:48 UTC] cataphract@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cataphract
 [2010-10-02 10:48 UTC] cataphract@php.net
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.


 [2010-10-02 10:50 UTC] cataphract@php.net
Sorry, the comment is wrong; the changes on the manual will be online only next Friday.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 29 14:00:03 2025 UTC