开发者

Parse CSV file of links to php array, feed these links to simplehtmldom

I have a php code that will read and parse csv files into a multiline array, what i need to do next is to take this array and let simplehtmldom fire off a crawler to return some company stocks info.

The php code for the CSV parser is

$arrCSV = array();  
// Opening up the CSV file  
if (($handle = fopen("NASDAQ.csv", "r")) !==FALSE) {  
// Set the parent array key to 0  
$key = 0;  
// While there is data available loop through unlimited times (0) using separator (,)
while (($data = fgetcsv($handle, 0, ",")) !==FALSE) {  
   // Count the total keys in each row $data is the variable for each line of the array
$c = count($data);  
   //Populate the array  
   for ($x=0;$x<$c;$x++) {  
   $arrCSV[$key][$x] = $data[$x];  
   }  
   $key++;  
} // end while  
// Close the CSV file  
fclose($handle);  
} // end if  
echo "<pre>";  
echo print_r($arrCSV);  
echo "</pre>";

This works great and parses the array line by line, $data being the variable for each line. What i need to do now is to get this to be read via simplehtmldom, which is where it breaks down, im looking at using this code or something very similar, im pretty inexperienced at this but guess i would be needing a foreach statement somewhere along the line.

This is the simplehtmldom code

$html = file_get_html($data);  
$html->find('div[class="detailsDataContainerLt"]');  
$tickerdetails = ("$es[0]");  
$FileHandle2 = fopen($data, 'w') or die("can't open file");  
fwrite($FileHandle2, $tickerdetails);  
fclose($FileHandle2);  
fclose($handle);  
开发者_运维知识库

So my qyestion is how can i get them both working together, i jave checked out simplehtmldom manual page several times and find it a littlebit vague in this area, the simplehtmldom code above is what i use in another function but by direclty linking so i know that it works.

regards Martin


Your loop could be reduced to (yes, it's the same):

while ($data = fgetcsv($handle, 0, ',')) {
    $arrCSV[] = $data;
}

Using SimpleXML instead of SimpleDom (Since it's standard PHP):

foreach ($arrCSV as $row) {
    $xml = simplexml_load_file($row[0]); // Change 0 to the index of the url
    $result = $xml->xpath('//div[contains(concat(" ", @class, " "), " detailsDataContainerLt")]');
    if ($result->length > 0) {
        $file = fopen($row[1], '2'); // Change 1 to the filename you want to write to
        if ($file) {
            fwrite($file, (string) $result->item(0));
            fclose($file);
        }
    }
}

that should do it if I understood correctly...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜