|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-02-24 22:26 UTC] pawel dot ulewicz at gmail dot com
Description:
------------
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:
---------------
<?php
// 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:
----------------
a2f
1def
2ef
Actual result:
--------------
1def
1def
2ef
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 17:00:01 2025 UTC |
I can confirm that, it's across platforms - strtr('abcdef', ['a'=> '1', 'bcde'=> '2']) works - strtr('abcdef', ['abc'=> '1', 'bcde'=> '2']) bug