Forum Moderators: coopster

Message Too Old, No Replies

eval() problem

won't replace $variables

         

jamie

8:22 pm on Sep 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

in my template system i inject the $content_variables into an htm template which i eval() so the $variables in it can be parsed.

however, when i have a $variable in the middle of a non-broken line, it is not parsed, e.g.:

href="/style/<?php echo $style01;?>.css works, but

/graphics/top_<?php echo $graphics_color;?>_06.gif does not

i am sure this has something to do with symbols / characters which specify a break of sorts, but i can't work it out?

any help is much appreciated

thanks!

bcolflesh

8:35 pm on Sep 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What happens if you escape the underscore in:

$graphics\_color

?

jamie

6:35 am on Sep 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sorry bcolflesh, i should have been more specific:

the <?php echo is not there, because it is an htm file which i am using (my mistake). my code for the eval is:

foreach( file("templates/$the_template.htm") as $line ) {
$main_template .= $line;
}
eval("\$str = \"" . str_replace("\"", "\\\"", $main_template) . "\";");

which escapes all my " in the html file.

now when i have a variable in the middle of an unbroken line:

top_$graphics_color_02.jpg

only top_02.jpg is displayed

but when i add a full stop:

top_.$graphics_color._02.jpg

top_.standard._02.gif is displayed.
i.e the $variable is being substituted correctly, but of course the full stops are still there!

the same thing happens when i escape \_ the underscore:

top\_$graphics_color\_02.jpg

becomes top\_standard\_02.jpg

thanks :)

<added>

after careful studying of the eval() manual page, i have solved it. i needed to escape the following undersore as you suggested and then stripslashes($main_template).

top_$graphics_color\_02.jpg

works perfectly!

many thanks for putting me in the right direction!

jamie

9:58 am on Sep 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



even better..... :)

i simply enclose the variables in {}!

wood for the trees?