views:

167

answers:

8

Here is what I am trying to do:

<?php
    $my_str = "My String";
    $str = "%my_str";

    $str = str_replace("%", "$", $str);

    echo $str;
?>

The above code prints '$my_str' to the screen. But I want it to print 'My String', as in the actual value of the variable $my_str

Anyone know how to do this?

The reason I want this is because I'm in the process of writing my own, very basic, parsing language so I kinda need this to be functional before I can continue.

A: 
  1 <?php
  2     $my_str = "My String";
  3     $str = "%my_str";
  4
  5     $str = str_replace("%", "$", $str);
  6
  7         eval("echo $str;");
  8 ?>
~
Noah
Thank you this works wonders!However what I really am after is: eval("\$str = \"$str\";");So I can do stuff with the string and print it at a later stage.Cheers for the help
You're welcome. I'll assume you're not the one down-voting. Someone in cyber-land doesn't like eval(). Oh yeah? Well, eval(), eval(), eval()!
Noah
Don't pay attention to the naysayers, eval() is perfectly fine! By the way, what is the name of the website you plan to use it? Just curious, I don't plan to exploit that HUGE HUGE SECURITY FLAW to take over your server of course.
Josh Davis
http://www.microsoft.com Have at it
Noah
+8  A: 
$my_str = 'My String';
$str    = 'my_str';

echo $$str;

This construction is called a variable variable.

Ionuț G. Stan
I'm not sure this is what the user wanted - they seem to want to perform interpolation of the string, as if it were a literal, rather than just getting the value of the variable named in the string.
Adam Wright
Indeed this is most certainly not what I am after. I'm completely aware of how variable variables work. Thanks for your help though.(Weird how this is voted up and the other answers, which are exactly what I was after, are voted down)
Honestly, I don't understand what you were after and I don't understand why `eval` was better in this case. Anyway, if `eval` is better for you, I'm fine with that.
Ionuț G. Stan
+1  A: 

eval is not necessary. Just get rid of the % and use $$str

<?php
    $my_str = "My String";
    $str = "%my_str";

    $str = str_replace("%", "", $str);

    echo $$str;
?>
Dooltaz
Thanks for the help but I want the %, as I said, I'm in the process of making a simple parsing language which processes templates. eval works great, but thanks for your help regardless!
A: 

The behaviour you see is not surprising - expansion of variables ("interpolation") is performed on only string literals, rather than variables. If it wasn't, then it would be a large security hole (any instance of a string with $ in it that your code used would suddenly be revealing variables).

You can try and fix this using eval, as in

eval ("\$str = \"" . str_replace("%", "\$", $str) . "\"");

but this pretty dangerous - if your string is from the user, then can make $str something like

"; system("rm -rf /"); $x = "

and suddenly, you're in trouble. The best solution, I believe, will be to parse out variables using your favourite methods (stripos and substring, or something more), then replace each one by hand.

Adam Wright
Actually that code gives me errors. eval("\$str = \"" . str_replace("%", "$", $str) . "\";");That works though, cheers for the help.
+1  A: 

Try:

echo $$str;
krdluzni
+1  A: 

You could search and replace the %var patterns using preg_replace and the e modifier, which makes the replacement being evaluated:

<?php
 $my_str = "My String";
 $str = "Foo %my_str bar";

 $str = preg_replace("/%([A-Za-z_]+)/e", "$\\1", $str);

 echo $str;
?>

Here preg_replace will find %my_str, \1 contains my_str and "$\\1" (the backslash needs to be escaped) becomes the value of $my_str.

However, it would maybe be cleaner to store your replacement strings in an associative array:

<?php
 $replacements = array(
  "my_str" => "My String",
 );
 $str = "Foo %my_str bar";

 $str = preg_replace("/%([A-Za-z_]+)/e", '$replacements["\\1"]', $str);

 echo $str;
?>
Ölbaum
A: 

PHP automatically parses and expands variables within double-quoted strings:

<?php
    $my_str = "My String";
    $str = "{$my_str}";

    echo $str;
?>

Outputs:

My String
shufler
A: 

How about using a regex with the e modifier

$str = preg_replace("/%([a-zA-Z0-9_]+)/e", '$\1', $str);

(This is untested, but should work)

MiffTheFox