|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66765 strtr key replacing order
Submitted: 2014-02-24 22:26 UTC Modified: 2017-10-26 17:22 UTC
Avg. Score:3.7 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: pawel dot ulewicz at gmail dot com Assigned:
Status: Open Package: Strings related
PHP Version: 5.5.9 OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: pawel dot ulewicz at gmail dot com
New email:
PHP Version: OS:


 [2014-02-24 22:26 UTC] pawel dot ulewicz at gmail dot com
It looks like strtr function replaces strings in the following order:

1) first occurences first,
2) longest keys first.

I don't think it's a bad thing, but it doesn't work as documented. Here's the quation from the official docs:

"The longest keys will be tried first"

So, it's either a documentation problem or a bug.

Test script:

// expecting 'bcde' to be replaced first
echo strtr('abcdef', ['abc'=> '1', 'bcde'=> '2']) . "\n";

// the following two are replaced as expected, just to give more info
echo strtr('abcdef', ['a'=> '2', 'abc'=> '1']) . "\n";
echo strtr('abcdef', ['abcd'=> '2', 'abc'=> '1']) . "\n";

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-02-28 11:45 UTC]
-Status: Open +Status: Analyzed -Assigned To: +Assigned To: ab
 [2014-02-28 11:45 UTC]
I can confirm that, it's across platforms

- strtr('abcdef', ['a'=> '1', 'bcde'=> '2']) works
- strtr('abcdef', ['abc'=> '1', 'bcde'=> '2']) bug
 [2016-10-12 15:40 UTC]
It seems to me this is a documentation issue. strtr() walks the
string from left to right, continously replacing the longest
matching sequence.
 [2017-10-24 05:46 UTC]
-Status: Analyzed +Status: Assigned
 [2017-10-26 17:22 UTC]
-Status: Assigned +Status: Open -Assigned To: ab +Assigned To:
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Thu Feb 02 22:03:49 2023 UTC