PHP 4.0 Bug #5768: patch to make parse_str() optionally save into an array
| From: | dshadow at zort dot net | Date: | Mon, 24 Jul 2000 21:37:34 +0000 |
| Subject: | PHP 4.0 Bug #5768: patch to make parse_str() optionally save into an array | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-25857@lists.php.net to get a copy of this message | ||
From: dshadow@zort.net
Operating system: Linux
PHP version: 4.0.1pl2
PHP Bug Type: Feature/Change Request
Bug description: patch to make parse_str() optionally save into an array
The following patch modifies parse_str() to optionally save variables into an array, rather than
into the current variable space.
Usage:
Normal:
parse_str(encodedString);
Saves variables into the output array, rather than into the current variable space:
parse_str(encodedString, outputArray);
This has been used in a production environment for at least six months with no problems. (The patch
was originally written for PHP3.) I'm hoping this can get included with PHP so I don't
have to keep repatching whenever a new PHP version is released.
John Bafford
dshadow@zort.net
--
*** string.old Mon Jul 24 17:14:32 2000
--- string.c Mon Jul 24 17:16:23 2000
***************
*** 2162,2184 ****
}
/* }}} */
! /* {{{ proto void parse_str(string encoded_string)
! Parses GET/POST/COOKIE data and sets global variables. */
PHP_FUNCTION(parse_str)
{
! zval **arg;
char *res = NULL;
PLS_FETCH();
SLS_FETCH();
!
! if (zend_get_parameters_ex(1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg);
! if ((*arg)->value.str.val && *(*arg)->value.str.val) {
! res = estrndup((*arg)->value.str.val,(*arg)->value.str.len);
}
! php_treat_data(PARSE_STRING, res ELS_CC PLS_CC SLS_CC);
}
/* }}} */
--- 2162,2227 ----
}
/* }}} */
! /* {{{ proto void parse_str(string encoded_string, [array storage])
! Parses GET/POST/COOKIE data and sets global variables.
! Stores into storage if array is given
! */
PHP_FUNCTION(parse_str)
{
! zval **arg, **arrayArg;
! zval *sarg;
char *res = NULL;
+ int argCount;
+
PLS_FETCH();
SLS_FETCH();
!
! argCount = ARG_COUNT(ht);
! if(argCount < 1 || argCount > 2 || zend_get_parameters_ex(argCount, &arg,
&arrayArg) == FAILURE) {
WRONG_PARAM_COUNT;
}
+
convert_to_string_ex(arg);
! sarg = *arg;
! if (sarg->value.str.val && *sarg->value.str.val) {
! res = estrndup(sarg->value.str.val, sarg->value.str.len);
}
!
! if(argCount == 1)
! php_treat_data(PARSE_STRING, res ELS_CC PLS_CC SLS_CC);
! else
! {
! if(!ParameterPassedByReference(ht, 2)){
! php3_error(E_WARNING, "Array not passed by reference in call to parse_str()");
! return;
! }
!
! //Clear out the array that was passed in
! pval_destructor(*arrayArg);
! array_init(*arrayArg);
!
! if(res) //hacked up php_treat_data
! {
! char *var, *val;
!
! var = strtok(res, PG(arg_separator));
!
! while (var) {
! val = strchr(var, '=');
! if (val) { /* have a value */
! *val++ = '\0';
! /* FIXME: XXX: not binary safe, discards returned length */
! php_url_decode(var, strlen(var));
! php_url_decode(val, strlen(val));
!
! add_assoc_string(*arrayArg, var, val, 1);
! }
! var = strtok(NULL, PG(arg_separator));
! }
! }
! }
!
! efree(res);
}
/* }}} */
*** basic_functions.old Mon Jul 24 17:20:49 2000
--- basic_functions.c Mon Jul 24 17:20:51 2000
***************
*** 169,175 ****
PHP_FE(levenshtein, NULL)
PHP_FE(chr, NULL)
PHP_FE(ord, NULL)
! PHP_FE(parse_str, NULL)
PHP_FE(str_pad, NULL)
PHP_FALIAS(rtrim, chop, NULL)
PHP_FALIAS(strchr, strstr, NULL)
--- 169,175 ----
PHP_FE(levenshtein, NULL)
PHP_FE(chr, NULL)
PHP_FE(ord, NULL)
! PHP_FE(parse_str, second_arg_force_ref)
PHP_FE(str_pad, NULL)
PHP_FALIAS(rtrim, chop, NULL)
PHP_FALIAS(strchr, strstr, NULL)