Doc #66765 [NEW]: strtr key replacing order

From: Date: Mon, 24 Feb 2014 22:26:21 +0000
Subject: Doc #66765 [NEW]: strtr key replacing order
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-11013@lists.php.net to get a copy of this message
From:             pawel dot ulewicz at gmail dot com
Operating system: Windows 7
PHP version:      5.5.9
Package:          Strings related
Bug Type:         Documentation Problem
Bug description:strtr key replacing order

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


-- 
Edit bug report at https://bugs.php.net/bug.php?id=66765&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=66765&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=66765&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=66765&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=66765&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=66765&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=66765&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=66765&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=66765&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=66765&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=66765&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=66765&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=66765&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=66765&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66765&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=66765&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=66765&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=66765&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66765&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=66765&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=66765&r=mysqlcfg



Thread (7 messages)

« previous php.doc.bugs (#11013) next »