JoshK32
JoshK32

Reputation: 33

php set lines in a text file to an array value

Im looking to do something like the following

$file = ('file.txt');
$fopen($file);

then read each line from the file individually and set it as a specific array

like so

read $file get $line1 set as $array[0]
read $file get $line2 set as $array[1]
read $file get $line3 set as $array[2]

I would like to use these arrays created from the lines on the text file IN PLAIN TEXT like this:

$urlout = file_get_contents("http://myurl.com/?=$line1");
echo $urlout;
$urlout2 = file_get_contents("http://myurl.com/?=$line2");
echo $urlout2;
$urlout3 = file_get_contents("http://myurl.com/?=$line3");
echo $urlout3;

So if the array were 123.22.11.22 the link would look like this:

$line1 = array[0] (123.22.11.22)
$urlout = file_get_contents("http://myurl.com/?=$line1");
echo $urlout;

and the result would be

Info for 123.22.11.22 more info some more

Upvotes: 0

Views: 64

Answers (3)

JoshK32
JoshK32

Reputation: 33

The answer was to insert a "\n" separator upon inserting into the text file, then removing it after the first result was called

$lines = file("geo.txt");
for($i=0 ; $i<count($lines); $i=($i+2) )
{
echo file_get_contents("https://test.com/api/GEO.php?info=".$lines[$i]);

$lolz = file_get_contents("https://test.com/api/GEO.php?info=".$lines[($i + 1)]);
$lolz = str_replace(" \n", '', $lolz);
echo "<br>".$lolz;

}

Upvotes: 0

Makesh
Makesh

Reputation: 1234

Modified answer as per the change indicated by the user :

Reading 2 lines on each loop..

$lines = file("file.txt");
for($i=0 ; $i<count($lines); $i=($i+2) )
{
    echo file_get_contents("http://myurl.com/?=".$lines[$i]);
    echo file_get_contents("http://myurl.com/?=".$lines[($i + 1)]);
}

Imp Note : A URL can be used as a filename with file_get_contents() function, only if the fopen wrappers have been enabled.

Upvotes: 3

OptimusCrime
OptimusCrime

Reputation: 14863

$handle = fopen("file.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        echo file_get_contents("http://myurl.com/?=$line");
    }

    fclose($handle);
}

Upvotes: 0

Related Questions