Jump to content

[SOLVED] Calling a Variable That is Stored as String


TripleDES

Recommended Posts

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.

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

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.

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";

}

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.

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

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.

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.

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.

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.

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!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.