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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
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
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

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC