|  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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
22 + 22 = ?
Subscribe to this entry?

 [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:
 [2023-04-26 06:46 UTC] vikashsjsharmag at gmail dot com
Finance Update Daily are sharing latest news about finance, business, loan, trading, share market, banking etc. More info to visit: (
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 01 12:01:29 2024 UTC