This should be easy, but I haven't figured it out yet.
PHP 4.2.2-8.0.5 is installed with httpd 2.0.40-8 on a system running
RedHat 8.0. I've configured it so that the "<? ?>" convention works,
and php code is being correctly evaluated by httpd.
Given that the pgp code performs calculations based on user input that
set variables var1, var2, var3, var4, etc.. I can see by "echo"
statements that the variables are getting set correctly.
Given that a text file addr.txt contains lines of the following
nature:
<img src="http://site1/$var1">
<img src="http://site2/$var2">
<img src="http://site3/$var3">
<img src="http://site4/$var4">
I'd like to use a loop like the following to parse the file and
display the images.
$fd = fopen ("addr.txt", "r");
while (!feof($fd)) {
$buffer = fgets($fd, 4096);
echo $buffer;
}
fclose ($fd);
Looking at the resulting html source, I see that the variable
substitution is not occurring. What's echoing is the literal string:
<img src="http://site1/$var1">
<img src="http://site2/$var2">
etc...
instead of the string with $var1 substituted by the contents of var1,
etc.
So, then I tried replacing $var1 in addr.txt with:
<? echo $var1 ?>
...but what shows up in the page source is:
<img src="http://site1/<? echo $var1 ?>">
What invocation do I need to use? Thanks in advance.
Ron
-
"I do not love the bright sword for its sharpness, nor the arrow
for its swiftness, nor the warrior for his glory. I love only
that which they defend."
http://roc85.home.attbi.com
You might try something like:
$filename = "addr.txt";
if (is_file ($filename)) { $src = file ($filename); }
else { echo "file error"; }
for ($i = 1; $i <= count ($src); $i++)
{
echo str_replace ("$var" . $i, $var1, $rtrim ($src[$i]);
$buffer='';
$fd = fopen ("addr.txt", "r");
while (! feof($fd)) {
$buffer .= fgets($fd, 4096);
}
fclose ($fd);
$buffer = preg_replace('/\$var(\d+)/e', '${"var\\1"}', $buffer);
echo $buffer;
regards,
reggie.
> <img src="http://site1/$var1">
> <img src="http://site2/$var2">
> <img src="http://site3/$var3">
> <img src="http://site4/$var4">
>
> I'd like to use a loop like the following to parse the file and
> display the images.
>
> $fd = fopen ("addr.txt", "r");
> while (!feof($fd)) {
> $buffer = fgets($fd, 4096);
> echo $buffer;
> }
> fclose ($fd);
>
> Looking at the resulting html source, I see that the variable
> substitution is not occurring. What's echoing is the literal string:
>
> <img src="http://site1/$var1">
eval is your friend
Another way is to add one line at the beginnign of the txt file :
<? echo <<<MARCEL
and two lines at the end :
MARCEL;
?>
Then your txt file contains correct php instructions and you can just
replace your loop by :
include("addr.txt");
--
P'tit Marcel
eval is indeed my friend. Eval doesn't like double quotes in the url,
but fortunately none of the urls contained whitespace, so I was able
to do away with them.
Thanks to everyone who responded. Turns out there's at least four
ways to do it, but putting an eval statement in my existing loop
required the least change.