Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to display urls in a loop?

0 views
Skip to first unread message

Ronald O. Christian

unread,
Nov 29, 2002, 3:43:34 PM11/29/02
to

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

Tyrone Slothrop

unread,
Nov 29, 2002, 6:37:33 PM11/29/02
to
I believe you would have to include() the file in order for PHP to
place the value instead of a literal. However, this would not
necessarily accomplish what you want.

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]);

reggie

unread,
Nov 29, 2002, 7:25:07 PM11/29/02
to
"Ronald O. Christian" <ro...@europa.com> wrote in message
news:rdjfuukagav3f91tl...@4ax.com...

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


$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.


P'tit Marcel

unread,
Nov 29, 2002, 6:03:04 PM11/29/02
to
Ronald O. Christian écrivit:

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

Ronald O. Christian

unread,
Nov 29, 2002, 11:53:49 PM11/29/02
to
On Sat, 30 Nov 2002 00:03:04 +0100, "P'tit Marcel"
<geonona...@centrale-lyon.org.invalid> wrote:
>eval is your friend

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.

0 new messages