Todor Donev
Todor Donev

Reputation: 11

PHP: Use every line from txt to do this?

Trying to write a script that gets a line from txt (line is url), goes to the url, searches and grabs specific data. Grabbing the data works, but i need to do this multiple times. This is the find and get snippet it works fine.

    include(dom.txt);
    $html = file_get_html('url here');
    foreach($html->find('a.live') as $e)
    echo (''.$e->innertext.'<br />');

Heres what ive done so far but i get an error "Warning: file_get_contents(Array)"

    <?php
    include ("dom.php");
    $file = fopen("urls.txt", "r");
    $i = 0;
    while (!feof($file)) {
    $line[] = fgets($file);
    }
    fclose($file);
    foreach ($line as $x){
    $html = file_get_html("$line");
    foreach($html->find('a.live') as $e)
    echo (''.$e->innertext.'<br />');
    }
    ?>

Could somebody help ? :(

Edit changed $html = file_get_html("$line"); with $html = file_get_html("$x"); but still get error

Edit 2 script works but i think it overwrites the results and is displaying only the last result

Upvotes: 1

Views: 168

Answers (3)

James Holderness
James Holderness

Reputation: 23001

This line:

$html = file_get_html("$line");

should assumedly be:

$html = file_get_html("$x");

And as mastergalen has pointed out, there's no reason to include the $x in quotes. It doesn't gain you anything and just makes more work for the interpreter.

It's also worth noting that fgets will include a newline in the return value, which you may need to trim from the url before processing it.

Upvotes: 1

Wiktor
Wiktor

Reputation: 3059

You could use file() function which takes file path as argument and returns array of lines.

 <?php
 $aLines = file('/path/to/file');

 foreach( ( is_array( $aLines) ? $aLines : array() ) as $sLine )
 {
      // do what you have to do
      $html = file_get_html($sLine);
      foreach($html->find('a.live') as $e)
           echo (''.$e->innertext.'<br />');
 }
 ?>

Upvotes: -1

Mastergalen
Mastergalen

Reputation: 4389

You are passing the array $line into the function file_get_html, which only accepts strings.

You do have the foreach loop set to loop through the array. Use file_get_html($x) instead.

Upvotes: 2

Related Questions