TripleDES Posted July 24, 2008 Share Posted July 24, 2008 How do I go about calling a variable that is stored as a string? For example, I have a variable called: $a = '$newvariable'; Now when I call upon $a, I'd like the value in $newvariable to be called instead of being interpreted as a string. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/ Share on other sites More sharing options...
akitchin Posted July 24, 2008 Share Posted July 24, 2008 these are called variable variables, and they're always fun: $foo = 'bar'; $newvariable = 'foo'; echo $newvariable.'<br />'; echo $$newvariable; EDIT: just noticed that you're storing the dollar sign as well. may want to remove that using SUBSTR(), or if you don't have too many of them already stored, just store the variable name. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-598969 Share on other sites More sharing options...
TripleDES Posted July 24, 2008 Author Share Posted July 24, 2008 Thanks for the quick reply. Okay, here's more information: $begin = '$c['NEW:'; $end = '\'][0]'; $newline = str_replace('[[', '', $line); $newline = str_replace(']]', '', $newline); echo "$begin$newline$end"; Gives me: $c['NEW:Array'][0] Instead of the value Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-598975 Share on other sites More sharing options...
akitchin Posted July 24, 2008 Share Posted July 24, 2008 i have no idea what you're referring to, simply because i don't know what $line is. looks like $newline is an array through. try this: exit('<pre>'.print_r(get_defined_vars(), TRUE)); see what you have sitting in each variable. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-598976 Share on other sites More sharing options...
cooldude832 Posted July 24, 2008 Share Posted July 24, 2008 why do u need to do this I have a feeling you never explored arrays? Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-598977 Share on other sites More sharing options...
TripleDES Posted July 24, 2008 Author Share Posted July 24, 2008 I didn't want to overcomplicate the question, but here it goes. I'm trying to pull values from a string (eventually will be a file) that contains delimiters within [[]]. The index from an array I'm pulling is exactly the name of [[]]. So in this case, [[THIS]] can be called with $i_ar['NEW:THIS][0] and return a value. I'm replacing [[ with $i_ar['NEW: and ]] with ][0]. So instead of displaying the string, I want the value of the array called. Hope this clarifies it a bit and thanks for the help!! $template = 'Replace [[THIS]] and [[THISTOO]]'; $parser = xml_parser_create (); $ipinfo = implode ("", file("xml.txt")); xml_parse_into_struct($parser, $ipinfo, $d_ar, $i_ar); preg_match_all('/\[\[.+?\]\]/', $template, $matches); $begin = '$i_ar['NEW:'; $end = '\'][0]'; foreach ($matches[0] as $line) { $newline = str_replace('[[', '', $line); $newline = str_replace(']]', '', $newline); echo "$begin$newline$end"; } Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-598986 Share on other sites More sharing options...
akitchin Posted July 24, 2008 Share Posted July 24, 2008 not sure i understand why you don't replace them directly: $newline = str_replace('[[', $begin, $line); $newline = str_replace(']]', $end, $newline); echo $newline; echo $$newline; PS: you're missing an escape in front of the single quote in $begin. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599019 Share on other sites More sharing options...
TripleDES Posted July 24, 2008 Author Share Posted July 24, 2008 not sure i understand why you don't replace them directly: $newline = str_replace('[[', $begin, $line); $newline = str_replace(']]', $end, $newline); echo $newline; echo $$newline; I tried it that way, but I get this. PHP Notice: Undefined variable Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599075 Share on other sites More sharing options...
akitchin Posted July 25, 2008 Share Posted July 25, 2008 try the line i gave you earlier: exit('<pre>'.print_r(get_defined_vars(), TRUE)); pop it at the bottom and see what comes out. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599147 Share on other sites More sharing options...
TripleDES Posted July 25, 2008 Author Share Posted July 25, 2008 [line] => [[THIS]] [newline] => $i_ar['NEW:THIS'][0] Running this gets me an undefined variable error: echo $$newline; But running this gets me the value I'm looking for: echo $i_ar['NEW:THIS'][0]; Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599220 Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 Look at the php eval() function http://uk2.php.net/manual/en/function.eval.php Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599455 Share on other sites More sharing options...
akitchin Posted July 25, 2008 Share Posted July 25, 2008 i would strongly urge against using the eval() function. change this: $begin = '$i_ar['NEW:'; to: $begin = 'i_ar['NEW:'; and you should get the results you're after. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599557 Share on other sites More sharing options...
TripleDES Posted July 25, 2008 Author Share Posted July 25, 2008 i would strongly urge against using the eval() function. change this: $begin = '$i_ar['NEW:'; to: $begin = 'i_ar['NEW:'; and you should get the results you're after. I tried that too, but no workie. By the way, why do you recommend against using eval()? $begin = 'i_ar[\'NEW:'; $end = '\'][0]'; echo $$newline; PHP Notice: Undefined variable Seems that will only work if I define $i_ar['NEW:THIS'][0]; beforehand. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599577 Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 http://uk2.php.net/manual/en/function.eval.php eval — Evaluate a string as PHP code From your original post. How do I go about calling a variable that is stored as a string? For example, I have a variable called: $a = '$newvariable'; Now when I call upon $a, I'd like the value in $newvariable to be called instead of being interpreted as a string. Any ideas? Sorry not read the recent posts in depth, just you original caught my eye. I find eval() quite useful when variables are stored in database records which I have used before. If it doesnt help just ignore. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599584 Share on other sites More sharing options...
TripleDES Posted July 25, 2008 Author Share Posted July 25, 2008 Sorry not read the recent posts in depth, just you original caught my eye. I find eval() quite useful when variables are stored in database records which I have used before. If it doesnt help just ignore. eval() does work, but I'd like to know why akitchin recommended against it. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599586 Share on other sites More sharing options...
akitchin Posted July 25, 2008 Share Posted July 25, 2008 the only reason i recommended against it is because unless you're very careful about it, you could open your application to serious security vulnerability. that's just paranoia, so it's just a matter of whether you yourself feel comfortable with it. Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599587 Share on other sites More sharing options...
TripleDES Posted July 25, 2008 Author Share Posted July 25, 2008 the only reason i recommended against it is because unless you're very careful about it, you could open your application to serious security vulnerability. that's just paranoia, so it's just a matter of whether you yourself feel comfortable with it. Got it. Thanks for your help and Neil's on this. I appreciate it!! Quote Link to comment https://forums.phpfreaks.com/topic/116475-solved-calling-a-variable-that-is-stored-as-string/#findComment-599648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.