Doc #66765 [Ana->Asn]: strtr key replacing order

From: Date: Tue, 24 Oct 2017 05:46:31 +0000
Subject: Doc #66765 [Ana->Asn]: strtr key replacing order
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-15134@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66765&edit=1 ID: 66765 Updated by: kalle@php.net Reported by: pawel dot ulewicz at gmail dot com Summary: strtr key replacing order -Status: Analyzed +Status: Assigned Type: Documentation Problem Package: Strings related Operating System: Windows 7 PHP Version: 5.5.9 Assigned To: ab Block user comment: N Private report: N Previous Comments: ------------------------------------------------------------------------ [2016-10-12 15:40:19] cmb@php.net It seems to me this is a documentation issue. strtr() walks the string from left to right, continously replacing the longest matching sequence. ------------------------------------------------------------------------ [2014-02-28 11:45:27] ab@php.net I can confirm that, it's across platforms - strtr('abcdef', ['a'=> '1', 'bcde'=> '2']) works - strtr('abcdef', ['abc'=> '1', 'bcde'=> '2']) bug ------------------------------------------------------------------------ [2014-02-24 22:26:20] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66765&edit=1

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