Bug #69022 [Nab]: strtr fails to consistently translate to an empty string
| From: | requinix@php.net | Date: | Tue, 10 Feb 2015 11:06:51 +0000 |
| Subject: | Bug #69022 [Nab]: strtr fails to consistently translate to an empty string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-190568@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69022&edit=1
ID: 69022
Updated by: requinix@php.net
Reported by: slevy1 at pipeline dot com
Summary: strtr fails to consistently translate to an empty
string
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.6.5
Block user comment: N
Private report: N
New Comment:
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().
Previous Comments:
------------------------------------------------------------------------
[2015-02-10 10:27:08] 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 10:24:59] 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 09:49:21] 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 04:17:56] 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 03:57:21] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69022&edit=1