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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC