php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9963 strtr behaves differently than unix tr
Submitted: 2001-03-23 21:55 UTC Modified: 2001-03-29 06:35 UTC
From: chris_abbey at yahoo dot com Assigned:
Status: Closed Package: Strings related
PHP Version: 4.0.2 OS: linux
Private report: No CVE-ID: None
 [2001-03-23 21:55 UTC] chris_abbey at yahoo dot com
$str = "abcdefghij";
$str = strtr($str, "abcdef", "XYZ");

results in "XYZdefghij"

where as in standard unix:

echo "abcdefghij" | tr 'abcdef' 'XYZ'

would output "XYZZZZghij" because the last character
is repeated automatically. Some versions will return
"XYZXYZghij" because they reuse the second string, but
I'm not if that's the "correct" behaviour, or if the
fisrt one is... for what I'm trying to accomplish it
doesn't matter though.

An example of why this is a problem...

$obj_nom = "The Foo-Object";
$file_nom = strtr($obj_nom, " -", ""); // or ..., '');

One would expect this to assign "TheFooObject" to
$file_nom... but instead no translation is performed.

This would also be the same as:

tr -d ' -'

which is logically equivalent to:

tr ' -' ''

There does not appear to be a native php function to
do the same thing, infact the editors of the user coments
have a couple times told people to use strtr or one of
the regexp replacement libraries to accomplish this...
not knowing that none of them seem to do so.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-25 18:23 UTC] chris_abbey at yahoo dot com
I'm using the preg_replace routine to get equivalent function.
 [2001-03-29 06:35 UTC] stas@php.net
The function works just like it's defined to work, so it's
not a bug. You can use any of the replace functions (fastest
would be str_replace, I guess) to achieve the -d functionality.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 07:01:28 2024 UTC