PHP4 parse_str patch
| From: | John Bafford | Date: | Sun, 25 Jun 2000 16:07:34 +0000 |
| Subject: | PHP4 parse_str patch | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-22273@lists.php.net to get a copy of this message | ||
Hi all..
Below is a patch I wrote originally for PHP3 and rewrote for PHP4 that modifies the parse_str() function to take an optional second argument. When provided, this second argument (which must be passed by reference) is treated as an associative array that receives the variables extracted from the first argument. The array is emptied before it is filled.
I've been using the PHP3 version of this for several months and it has worked nicely. I haven't been able to test the PHP4 version as extensively, but it at least works in simple test cases.
Hope this is useful for at least one other person.
John Bafford
dshadow@zort.net
---BEGIN PATCH php4-parsestr.patch
*** string-old.c Mon Jun 19 16:11:59 2000
--- string.c Mon Jun 19 16:11:01 2000
***************
*** 2128,2151 ****
}
/* }}} */
! /* {{{ 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); } /* }}} */ #define PHP_TAG_BUF_SIZE 1023 --- 2128,2195 ---- } /* }}} */ ! ! /* {{{ proto void parse_str(string encoded_string, array storage)
Parses GET/POST/COOKIE data and sets global variables. */+ 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); } + /* }}} */ #define PHP_TAG_BUF_SIZE 1023 ---END PATCH