解析CSV文件的链接到PHP阵列,将这些链接喂给simplehtmldom
问题描述:
我有一个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链接,所以我知道它的工作原理。
问候 马丁
答
你的循环可以降低到(是的,这是相同的):
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);
}
}
}
如果我理解正确,应该这样做...