2011-01-21 60 views
0

我有一個PHP代碼,它將讀取和解析csv文件到一個多行數組中,接下來我需要做的就是取這個數組,並讓simplehtmldom啓動一個履帶回歸一些公司股票信息。解析CSV文件的鏈接到PHP陣列,將這些鏈接餵給simplehtmldom

用於CSV解析器PHP代碼是

$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>"; 

這工作很大並解析由線的陣列線,$數據用於每行的變量。我現在需要做的是通過simplehtmldom來讀取它,這是它破壞的地方,即時查看使用此代碼或類似的東西,我很缺乏這方面的經驗,但是我猜想我需要在某處的foreach語句該線。

這是simplehtmldom代碼

$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); 

所以我qyestion是我怎樣才能讓他們兩個一起工作,我JAVE簽出simplehtmldom手冊頁幾次,發現這是一個littlebit在這方面的模糊,simplehtmldom上面的代碼是我在另一個函數中使用,但通過direclty鏈接,所以我知道它的工作原理。

問候 馬丁

回答

0

你的循環可以降低到(是的,這是相同的):

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

使用SimpleXML代替SimpleDom(因爲它是標準的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); 
     } 
    } 
} 

如果我理解正確,應該這樣做...