php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69022 strtr fails to consistently translate to an empty string
Submitted: 2015-02-10 03:57 UTC Modified: 2015-02-10 11:06 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: slevy1 at pipeline dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.5 OS:
Private report: No CVE-ID: None
 [2015-02-10 03:57 UTC] slevy1 at pipeline dot com
Description:
------------
If I provide strtr a from value like a comma, and a to value like an empty string, since the from value is longer than the to value, the translation fails to occur.

However, if I provide the from and to values in an associative array, then the comma in the from value is replaced by an an empty string in the to value.

Test script:
---------------
<?php

$nums = "1,2,3,\n,4,5,6,\n,7,8,9";
echo strtr($nums,array("," => ""));

echo "\n\n";

echo strtr($nums,",","");

Expected result:
----------------
123
456
789

123
456
789

Actual result:
--------------
123
456
789

1,2,3,
,4,5,6,
,7,8,9

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-10 04:17 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-02-10 04:17 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

That's right.

http://php.net/manual/en/function.strtr.php
>If from and to have different lengths, the extra characters in the longer of the
>two are ignored. The length of str will be the same as the return value's.
 [2015-02-10 09:49 UTC] slevy1 at pipeline dot com
I fail to understand why it is not a bug when strtr translates a comma to an empty str if one wraps this info in an array and does not if one neglects to use an array.  As for your nice suggestions about reading the manual, I did and then I examined the internal source code.  php_strtr and php_strtr_array may be invoked in the PHP function strtr (see http://lxr.php.net/xref/PHP_5_2/ext/standard/string.c#2844).  The problem is that php_strtr and php_strtr_array don't behave the same way when PHP's strtr is given a comma to translate to the empty string.  You may not call that a bug, but I call that one pesky inconsistency that really ought to be examined and fixed.
 [2015-02-10 10:24 UTC] nikic@php.net
-Status: Not a bug +Status: Re-Opened
 [2015-02-10 10:24 UTC] nikic@php.net
@requinix: The manual note refers to strtr in its character translation capabilities, not the the string replacement functionality that is used here.
 [2015-02-10 10:27 UTC] nikic@php.net
-Status: Re-Opened +Status: Not a bug
 [2015-02-10 10:27 UTC] nikic@php.net
Oh sorry, I misread this. You're of course right, everything is behaving as it is supposed to. Using translation with a different number of characters makes no sense.
 [2015-02-10 11:06 UTC] requinix@php.net
The comma has nothing to do with it - could be "1" or "\n" and you'd get the exact same output (in the latter call, that is).
http://3v4l.org/iBZK2

strtr(string, string, string) is transliteration like sed's y/ or Perl's tr/ where each Nth character in the second string is replaced with the Nth character in the third string. If the two strings aren't the same length then the "extra" characters in the longer one are ignored; in your case you have "," (length 1) and "" (length 0) so strtr() only uses the first 0 characters and thus does nothing.

strtr(string, array) is basically a str_replace() with each key/value pair in the array.

  strtr($nums, "1,2,3,", "abc") // abcb3b\nb4b5b6b\nb7b8b9
  strtr($nums, "1,2",    "abc") // abcb3b\nb4b5b6b\nb7b8b9

  strtr($nums, array("1,2,3," => "abc")) // abc\n,4,5,6,\n,7,8,9
  strtr($nums, array("1,2"    => "abc")) // abc,3,\n,4,5,6,\n,7,8,9

Speaking of str_replace(),
  str_replace(",", "", $nums)
produces your expected output so maybe you mean to use that instead of strtr().
 [2015-02-10 21:09 UTC] slevy1 at pipeline dot com
I meant strtr(), not str_replace().  If you look at the way Perl, Ruby, and Python manage "translation" it's very clear in each language, one function that does one thing one way consistently.  PHP's strtr is an umbrella function for two very different ways of handling translation or transliteration of strings; PHP doesn't have a char data type.  I think it would be less confusing for users to break up strtr into two PHP functions, strtr based on php_strtr following Perl's lead.  PHP could also have strtr_array based on php_strtr_array producing results the way Ruby does. In ea case there should be clear, accurate documentation about the function args, behavior and the results one should expect.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 10:01:31 2024 UTC