php.net |  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
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:
MUST BE VALID
Solve the problem:
33 + 20 = ?
Subscribe to this entry?

 
 [2012-06-11 12:23 UTC] a at b dot c dot de
Description:
------------
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');

print_r($translated);

Expected result:
----------------
Array
(
    [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Nov 16 22:01:35 2019 UTC