Reputation:
Instead of simple numbers 1-10,
I want to take information from a .txt file from the beginning of the file to the end, and go through it in the loop.
Example of txt file:
sport
news
cartoon
I want the loop to be like that:
$file = "readinfo.txt";
for($i=1;$i<=10;$i++){
$content[$i] = @get_file("http://website.com/$i");
if($content[$i]===FALSE){
echo "Error getting ('.$i.')<br>";
return;
}else{
echo "Got ('.$i.')<br>";
}
ob_flush();
_flush();
}
Upvotes: 0
Views: 110
Reputation: 95
Why dont you use a foreach?
$filename = 'readinfo.txt';
$i = 0;
$contents = file($filename);
foreach($contents as $line) {
$content[$i] = "http://website.com/".$line;
$i = $i + 1;
}
Upvotes: 0
Reputation: 618
<?php
$file = file_get_contents('readinfo.txt', true);
$exp=explode("\n", $file);
foreach($exp as $value){
echo $value."\n"; // this is one row
}
?>
Upvotes: 0
Reputation: 17434
The file
function will read a file into an array, e.g.
$file = "readinfo.txt";
$lines = file($file, FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
...
}
The FILE_IGNORE_NEW_LINES
flag is used to trim the new-line character(s) from the end of the lines, since all you're interested in are the individual words.
Depending on your file, you might also want to use the FILE_SKIP_EMPTY_LINES
flag.
Upvotes: 2