Skip to content

Change behavior of strtr(["" => "x"]) #4792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -3024,8 +3024,8 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p
} else {
len = ZSTR_LEN(str_key);
if (UNEXPECTED(len < 1)) {
efree(num_bitset);
RETURN_FALSE;
php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
continue;
} else if (UNEXPECTED(len > slen)) {
/* skip long patterns */
continue;
Expand Down Expand Up @@ -3499,6 +3499,7 @@ PHP_FUNCTION(strtr)
}
replace = zval_get_tmp_string(entry, &tmp_replace);
if (ZSTR_LEN(str_key) < 1) {
php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
RETVAL_STR_COPY(str);
} else if (ZSTR_LEN(str_key) == 1) {
RETVAL_STR(php_char_to_str_ex(str,
Expand Down
15 changes: 15 additions & 0 deletions ext/standard/tests/strings/strtr_empty_search_string.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
strtr() trying to replace an empty string
--FILE--
<?php

var_dump(strtr("foo", ["" => "bar"]));
var_dump(strtr("foo", ["" => "bar", "x" => "y"]));

?>
--EXPECTF--
Warning: strtr(): Ignoring replacement of empty string in %s on line %d
string(3) "foo"

Warning: strtr(): Ignoring replacement of empty string in %s on line %d
string(3) "foo"