note 25065 modified in function.parse-str by jmcastagnetto
| From: | jmcastagnetto@php.net | Date: | Tue, 10 Sep 2002 19:02:01 +0000 |
| Subject: | note 25065 modified in function.parse-str by jmcastagnetto | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-36466@lists.php.net to get a copy of this message | ||
[Editor's note: extract() and assignment to $GLOBALS should also work]
if you need to use parse_str in the scope of a function, this is the only way I have found to
globalize the parsed variables:
function globalizeVarstring($varstring)
{
parse_str($varstring,$buff);
foreach($buff as $variable => $contents)
{
$parse = 'global $'.$variable.'; ';
$parse .= '$'.$variable.' = ';
if(is_array($contents))
{
ob_start();
var_export($contents);
$parse .= ob_get_contents();
ob_end_clean();
}
elseif(is_string($contents))
{
$parse .= '"'.$contents.'"';
}
else
{
$parse .= $contents;
}
$parse .= ';';
eval($parse);
}
return TRUE;
}
globalizeVarstring(str_replace("\n","","
ex[image][action]=finish&ex[id]=12&
ex[image][1][title]=some scrolling image&
ex[image][1][type]=scrolling&
ex[image][2][title]=the main image ...&
ex[image][2][type]=main&
string=something"));
if there is an easier way that would be nice.
--was--
if you need to use parse_str in the scope of a function, this is the only way I have found to
globalize the parsed variables:
function globalizeVarstring($varstring)
{
parse_str($varstring,$buff);
foreach($buff as $variable => $contents)
{
$parse = 'global $'.$variable.'; ';
$parse .= '$'.$variable.' = ';
if(is_array($contents))
{
ob_start();
var_export($contents);
$parse .= ob_get_contents();
ob_end_clean();
}
elseif(is_string($contents))
{
$parse .= '"'.$contents.'"';
}
else
{
$parse .= $contents;
}
$parse .= ';';
eval($parse);
}
return TRUE;
}
globalizeVarstring(str_replace("\n","","
ex[image][action]=finish&ex[id]=12&
ex[image][1][title]=some scrolling image&
ex[image][1][type]=scrolling&
ex[image][2][title]=the main image ...&
ex[image][2][type]=main&
string=something"));
if there is an easier way that would be nice.
http://www.php.net/manual/en/function.parse-str.php