Forum Moderators: coopster

Message Too Old, No Replies

Evaluating a variable in a string

tyring a different method

         

baze22

12:17 am on Oct 25, 2007 (gmt 0)

10+ Year Member



I hope I'm overlooking something simple.

With this code, is there an easy way to get str2 to evaluate $mystr, like I can with str1?

<?
$mystr = 'test';
$str1 = 'This is a $mystr<br />';
$str2 = 'This is another <?=$mystr;?><br />';

eval("echo \"$str1\";");
eval("echo \"$str2\";");
?>

Thanks for any help.

eelixduppy

12:20 am on Oct 25, 2007 (gmt 0)



Why wouldn't you just do something like this?:

$mystr = 'test';
$str1 = 'This is a '.$mystr.'<br />';
$str2 = 'This is another '.$mystr.'<br />';
echo $str1;
echo $str2;

baze22

2:30 am on Oct 25, 2007 (gmt 0)

10+ Year Member



Why wouldn't you just do something like this?

That would be simpler, wouldn't it. :)

Actually, I'm trying to add a function to the template library I'm using, so that I can store the templates in a database as opposed to a file. I just put up a simplified example of what I'm trying to accomplish.

I could probably do it as a dynamically generated include file, but I'd rather not do it that way.

Habtom

7:01 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about replacing the opening and closing tags with something else, and converting back to the tags when needed.

And I think you might be loading eval with major tasks when you shouldn't.

>> Rasmus Lerdorf says "If eval() is the answer, you're almost certainly asking the wrong question.

Habtom

henry0

11:50 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Due to the thread I opened the manual
If mismanaged eval() could lead to security problem

Manual [us.php.net]

baze22

3:19 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



How about replacing the opening and closing tags with something else, and converting back to the tags when needed.

While that would be an option for a simple replacement as in the example I posted, I really wanted to keep the PHP functionality (conditionals, loops, function execution, etc) that I have in the template library.

As for the insecurity of the eval() function, I don't remember ever using it in production code before, but was considering that it could be a possibility in this case.

But my best option might be use a different library for this. I'd still like to know if it is doable.

Thanks for the input.