I've done this recently, it required a lot of work to get it to properly work :/
You have too split the variable name and the array-bits, and the do an eval ("return $var;");.
Here's a function I made for the purpose of converting variable- and array-names to global names:
Code: Select all
function parser_var2globals ($string) {
$string = preg_replace ('#\$([a-zA-Z0-9_\-]+)#', '$GLOBALS[\\1]', $string);
return $string;
}
If you want to return the value of the variable immidiately, swap the return-line with this:
Code: Select all
return eval ('return '.$string.';');
What it does is that it looks after patterns matching "${varname}" and replaces them with "$GLOBALS[{varname}]". This works for arrays too, since it stops after the name of the variable, for example: "${varname}[array][1]" is replaced with "$GLOBALS[{varname}][array][1]". The [array][1] stays, it's just the {varname} which gets replaced.
EDIT: feyd's method seems to be a little better, as you recieve an reference to the variable. That's what I myself tried to do from the start, put I couldn't manage to find a way to do it. Anyway, my method is at least less code
