简体   繁体   中英

Parsing with PHP Simple HTML DOM: URLs from CSV but in array just last URL from the list

Trying to get all products from specific page with URLs from CSV but it's not working right. There are three arrays:

  1. $test_base
  2. $base - made from the CSV file
  3. $words

It works right just with $test_base , other ones returns empty arrays except last one LINK. And I just don't understand because this LINKS arrays are identical.

include_once('simple_html_dom.php');

// Getting links array
$base = file("base.txt");

$words = array();

foreach($base AS $word) {
    $words[] = $word;
}

$test_base= array("LINK_1","LINK_2","LINK_3");

// Arrays are exactly the same
print_r($test_base);
print_r($base);
print_r($words);

// Main loop for one link
foreach($test_base AS $word) {

     $html = file_get_html($word);

// Getting all info for one link    
foreach($html->find('div.item_info') as $article) {
        $item['title'] = $article->find('.item_name', 0)->plaintext;
        $item['manufacture'] = $article->find('.item_additional-info', 0)->plaintext;
        $item['price'] = $article->find('.price span', 0)->plaintext;

   $articles[] = $item;
}

        echo "<pre>";
        print_r($articles);
        echo "</pre>";

$html->clear();
unset($html);

}

I have added a FILE_IGNORE_NEW_LINES parameter to the file() function, and its removed a spaces at the end of the string. And now all works like it should. Thanks to all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM