|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62291 Mixed first argument for strtr
Submitted: 2012-06-11 12:23 UTC Modified: -
From: a at b dot c dot de Assigned:
Status: Open Package: Strings related
PHP Version: 5.4.3 OS: Any
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: a at b dot c dot de
New email:
PHP Version: OS:


 [2012-06-11 12:23 UTC] a at b dot c dot de
While preg_replace and str_replace can both apply changes to arrays of strings, strtr can only be passed one string at a time for translation; it can only be called as strtr(string, string, string) or strtr(string, array).

This suggestion/request is for strtr to be support being called as, for example, strtr(array, string1$, string2$) (which would be effectively equivalent to array_map(function($w)use($string1, $string2) { return strtr($w, $string1, $string2); }, $array)).

This would bring it into closer line with the other "string-search-and-replace" functions.

Test script:
$words = ['this', 'is', 'a', 'bunch', 'of', 'words', 'with', 'no', 'rhythm'];

$translated = strtr($words, 'aeiou', 'AEIOU');


Expected result:
    [0] => thIs
    [1] => Is
    [2] => A
    [3] => bUnch
    [4] => Of
    [5] => wOrds
    [6] => wIth
    [7] => nO
    [8] => rhythm

Actual result:
A warning about a parameter 1 being an array instead of the expected string.


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 22:01:31 2024 UTC