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

From: Date: Tue, 10 Feb 2015 09:49:21 +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-190565@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 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. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#190565) next »