Bug #69022 [Com]: strtr fails to consistently translate to an empty string

From: Date: Tue, 10 Feb 2015 21:09:34 +0000
Subject: Bug #69022 [Com]: strtr fails to consistently translate to an empty string
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190571@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 Comment by: slevy1 at pipeline dot com 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: 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. Previous Comments: ------------------------------------------------------------------------ [2015-02-10 11:06:51] 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 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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=69022 -- Edit this bug report at https://bugs.php.net/bug.php?id=69022&edit=1

« previous php.bugs (#190571) next »