Forum Moderators: coopster
I want to do some data extraction from a website with variable urls. There is a fixed part in the URL ( e.g. [site.com...] and a variable part which is a number (e.g. 100). I want to extract all the html pages from 100 till 110.
I used the script below for the data extraction of a single page :
<?php
$lines = file ('http://www.example.com/');
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";
}
I adjusted this script like this :
<?php
$a=100;
$b=110;
while ($a <= $b)
{
$lines = file ('http://www.example.com/show.php?id=$a');
foreach ($lines as $line_num => $line) {
echo "" . htmlspecialchars($line) . "<br>\n";
}
$a++;
}
?>
The output of the script gives me 10 times the data from the url 'http://www.example.com/show.php?id=$a' instead of 'http://www.example.com/show.php?id=100' till 'http://www.example.com/show.php?id=110'. How can I tell PHP to use the variable $a in the url?
Thx,
Turbo-host
$dest = 'http://www.example.com/show.php?id=' . $a;
$lines = file($dest);
should work fine but I would think that it isn't resolving the variable because it is in single quotes instead of double. So this should fix it.
$lines = file("http://www.example.com/show.php?id=$a");
<added>I am a little slow this morning it seems ;)
<?php
$a=100;
$b=102;
while ($a <= $b)
{
$lines = file ("http://www.example.com/show.php?id=$a");
foreach ($lines as $line_num => $line)
{
$file_content = . htmlspecialchars($line) . "<br>\n";
$file_open = fopen('/home/bla/public_html/bla.txt', w);
fwrite($file_open, $file_content);
}
$a++;
}
?>
Here is the working script if somebody else wants to use it.
<?php
$a=100;
$b=102;
while ($a <= $b)
{
$lines = file ("http://www.example.com/show.php?id=$a");
foreach ($lines as $line_num => $line)
{
$file_content .= htmlspecialchars($line) . "<br>\n";
}
$file_open = fopen('/home/bla/public_html/bla.txt', w);
fwrite($file_open, $file_content);
fclose($file_open);
$a++;
}
?>