php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch php-trunk-substr_replace-array-keys for Strings related Bug #55366

Patch version 2011-08-05 10:20 UTC

Return to Bug #55366 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: arpad@php.net

Index: ext/standard/string.c
===================================================================
--- ext/standard/string.c	(revision 314290)
+++ ext/standard/string.c	(working copy)
@@ -2398,6 +2398,10 @@
 			RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);	
 		}
 	} else { /* str is array of strings */
+		char *str_index = NULL;
+		uint str_index_len;
+		ulong num_index;
+
 		array_init(return_value);
 
 		if (Z_TYPE_PP(from) == IS_ARRAY) {
@@ -2533,7 +2537,13 @@
 			}
 
 			result[result_len] = '\0';
-			add_next_index_stringl(return_value, result, result_len, 0);
+
+			if (zend_hash_get_current_key_ex(Z_ARRVAL_PP(str), &str_index, &str_index_len, &num_index, 0, &pos_str) == HASH_KEY_IS_STRING) {
+				add_assoc_stringl_ex(return_value, str_index, str_index_len, result, result_len, 0);
+			} else {
+				add_index_stringl(return_value, num_index, result, result_len, 0);
+			}
+
 			if(Z_TYPE_PP(tmp_str) != IS_STRING) {
 				zval_dtor(orig_str);
 			}
Index: ext/standard/tests/strings/substr_replace_array.phpt
===================================================================
--- ext/standard/tests/strings/substr_replace_array.phpt	(revision 0)
+++ ext/standard/tests/strings/substr_replace_array.phpt	(revision 0)
@@ -0,0 +1,19 @@
+--TEST--
+substr_replace() function - array
+--FILE--
+<?php
+
+$arr = array('abc' => 'llsskdkk','def' => 'llsskjkkdd', 4 => 'hello', 42 => 'world');
+$newarr = substr_replace($arr, 'zzz', 0, -2);
+
+print_r($newarr);
+
+?>
+--EXPECTF--
+Array
+(
+    [abc] => zzzkk
+    [def] => zzzdd
+    [4] => zzzlo
+    [42] => zzzld
+)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC