[PATCH] New function implode_multi

From: Date: Fri, 07 Jun 2002 08:49:21 +0000
Subject: [PATCH] New function implode_multi
Groups: php.dev 
Request: Send a blank email to php-dev+get-84974@lists.php.net to get a copy of this message
Hi there, while creating some stuff in PHP, I needed the PHP function implode() work like Perl function join, so that the first argument of the function is glue and an unlimited list of other arguments is made of arrays and strings. So I made a new function called implode_multi (with an alias join_multi). The code works fine and correctly, here is an example: <? $fruits = array('banana', 'orange', 'apple'); $vegetables = array('carrot', 'cabbage', 'cucumber'); $favorite_food = 'icecream'; $all_day_food = implode_multi(', ', $fruits, $vegetables, $favorite_food); // ... = "banana, orange, apple, carrot, cabbage, cucumber, icecream" ?> -- Bye ... C. McCohy The killings will not stop until you Karel Roden ----------------------------------------------------------------------- diff -aur php-4.2.1.orig/ext/standard/basic_functions.c php-4.2.1/ext/standard/basic_functions.c --- php-4.2.1.orig/ext/standard/basic_functions.c Sat May 11 21:23:05 2002 +++ php-4.2.1/ext/standard/basic_functions.c Fri Jun 7 09:30:40 2002 @@ -339,6 +339,7 @@ PHP_FE(similar_text, third_arg_force_ref) PHP_FE(explode, NULL) PHP_FE(implode, NULL) + PHP_FE(implode_multi, NULL) PHP_FE(setlocale, NULL) PHP_FE(localeconv, NULL) @@ -576,6 +577,7 @@ PHP_FE(split, NULL) PHP_FE(spliti, NULL) PHP_FALIAS(join, implode, NULL) + PHP_FALIAS(join_multi, implode_multi, NULL) PHP_FE(sql_regcase, NULL) /* functions from dl.c */ diff -aur php-4.2.1.orig/ext/standard/php_string.h php-4.2.1/ext/standard/php_string.h --- php-4.2.1.orig/ext/standard/php_string.h Thu Feb 28 09:26:48 2002 +++ php-4.2.1/ext/standard/php_string.h Thu Jun 6 15:55:59 2002 @@ -37,6 +37,7 @@ PHP_FUNCTION(wordwrap); PHP_FUNCTION(explode); PHP_FUNCTION(implode); +PHP_FUNCTION(implode_multi); PHP_FUNCTION(strtok); PHP_FUNCTION(strtoupper); PHP_FUNCTION(strtolower); diff -aur php-4.2.1.orig/ext/standard/string.c php-4.2.1/ext/standard/string.c --- php-4.2.1.orig/ext/standard/string.c Thu Apr 25 16:52:58 2002 +++ php-4.2.1/ext/standard/string.c Fri Jun 7 09:43:13 2002 @@ -874,6 +874,111 @@ } /* }}} */ +/* {{{ proto string join_multi(string glue, mixed var [, mixed var [, ...]]) + An alias for implode_multi */ +/* }}} */ + +/* {{{ proto string implode_multi(string glue, mixed var [, mixed var [, ...]]) + Joins elements with a string, if element is an array, joins its pieces */ +PHP_FUNCTION(implode_multi) +{ + zval ***args; + int argc; + zval *delim; + char *result_str; + int i, len = 0, target = 0; + + argc=ZEND_NUM_ARGS(); + if (argc < 2) { + WRONG_PARAM_COUNT; + } + + args = (zval ***) emalloc(argc * sizeof(zval **)); + + if (zend_get_parameters_array_ex(argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + } + + if (Z_TYPE_PP(args[0]) != IS_STRING) { + php_error(E_WARNING, "First argument to %s() must be string", + get_active_function_name(TSRMLS_C)); + return; + } + + convert_to_string_ex(args[0]); + delim = *args[0]; + + for (i = 1; i < argc; i++) { + zval *actarg; + actarg = *args[i]; + + if (Z_TYPE_PP(args[i]) == IS_ARRAY) { + int count = 0; + HashPosition pos; + zval **tmp; + + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(actarg), &pos); + while (zend_hash_get_current_data_ex(Z_ARRVAL_P(actarg), (void **) &tmp, &pos) == SUCCESS) { + convert_to_string_ex(tmp); + len += Z_STRLEN_PP(tmp); + count++; + zend_hash_move_forward_ex(Z_ARRVAL_P(actarg), &pos); + } + + if (count > 0) { + len += (count-1)*Z_STRLEN_P(delim); + } + + } else { + convert_to_string_ex(args[i]); + len += Z_STRLEN_PP(args[i]); + } + if (i > 1) { + len += Z_STRLEN_P(delim); + } + } + + result_str = (char *) emalloc (len + 1); + result_str[0] = 0; + + for (i = 1; i < argc; i++) { + zval *actarg; + actarg = *args[i]; + + if (i > 1) { + memcpy(result_str + target, Z_STRVAL_P(delim), Z_STRLEN_P(delim)); + target += Z_STRLEN_P(delim); + } + + if (Z_TYPE_PP(args[i]) == IS_ARRAY) { + int count = 0; + HashPosition pos; + zval **tmp; + + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(actarg), &pos); + while (zend_hash_get_current_data_ex(Z_ARRVAL_P(actarg), (void **) &tmp, &pos) == SUCCESS) { + if (count > 0) { + memcpy(result_str + target, Z_STRVAL_P(delim), Z_STRLEN_P(delim)); + target += Z_STRLEN_P(delim); + } + count++; + memcpy(result_str + target, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + target += Z_STRLEN_PP(tmp); + zend_hash_move_forward_ex(Z_ARRVAL_P(actarg), &pos); + } + + } else { + memcpy(result_str + target, Z_STRVAL_P(actarg), Z_STRLEN_P(actarg)); + target += Z_STRLEN_P(actarg); + } + } + + result_str[len] = 0; + RETURN_STRINGL(result_str, len, 0); +} +/* }}} */ + #define STRTOK_TABLE(p) BG(strtok_table)[(unsigned char) *p] /* {{{ proto string strtok([string str,] string token)

« previous php.dev (#84974) next »