-3

Possible Duplicate:
how to extract data from csv file in php

i'm new on php and now i try to make a private site for me and read out some stock information over yahoo api

Link: finance.yahoo.com/d/quotes.csv?s=^GDAXI+^TECDAX+eurusd=x+gcf12.cmx+CLH12.NYM&f=nl1k2

The link works fine but now how can i read this file out? And echo the information on my page?

Output:

"DAX",6864.43,"+54.97 - +0.81%"
"TECDAX",775.33,"+3.78 - +0.49%"
"EUR to USD",1.3447,"N/A - 0.00%"
"Gold Jan 12",1731.80,"N/A - +0.32%"
"Crude Oil Mar 12",105.88,"N/A - +0.04%"

What i need:

echo $name;
echo $rate;
echo $change;
echo $changeinpercent;

I hope some one can help me, and sry for my bad english.

Greetings, matthias

Community
  • 1
  • 1
Sebastian
  • 494
  • 5
  • 18
  • 34

4 Answers4

3

Have you tried looking at str_getcsv or fgetscsv?

nderjung
  • 1,607
  • 4
  • 17
  • 38
3

If you are using PHP5.3 you can use str_getcsv although you will still need to parse the last string value as that contains two values you are looking for (hint: explode).

If you're not on PHP5.3, you should be able to use fgetcsv with a stream passed to it.

liquorvicar
  • 6,081
  • 1
  • 16
  • 21
2

You could use RegEx to extract from each line the information you need. The following php code is what you need to extract this specific data. It's a RegEx code and you should use it with a preg_match function:

/\"([^\"]+)\",(\d+\.\d+),\"(N\/A)?([+-]?\d+\.?\d+)?\s+-\s+([+-]?\d+\.?\d+%?)/  

It gives you an array, and you will use it as:

$match[0]  
$match[1]  
$match[2]  
$match[3]  

UPDATE:
This is the code to do it:

<?php
$csv=file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=^GDAXI+^TECDAX+eurusd=x+gcf12.cmx+CLH12.NYM&f=nl1k2");

$rows=explode("\n", $csv);

for($i=0;$i<count($rows);$i++)
{
    preg_match_all("/\"([^\"]+)\",(\d+\.\d+),\"(N\/A)?([+-]?\d+\.?\d+)?\s+-\s+([+-]?\d+\.?\d+%?)/", $rows[$i],$matches,PREG_SET_ORDER);
}
?>  

It returns:

Array
(
   [0] => Array
        (
            [0] => "DAX",6864.43,"+54.97 - +0.81%
            [1] => DAX
            [2] => 6864.43
            [3] => 
            [4] => +54.97
            [5] => +0.81%
        )  
Array
(
    [0] => Array
        (
            [0] => "Gold Jan 12",1731.80,"N/A - +0.32%
            [1] => Gold Jan 12
            [2] => 1731.80
            [3] => N/A
            [4] => 
            [5] => +0.32%
        )

)

If it is N/A you will find it in $matches[3], if a value in 4. )

Andrew
  • 6,254
  • 16
  • 59
  • 93
1

first of all, you should see the manual here and then, see the following code pasted as example,

<?php 
// 
 // Convert csv file to associative array: 
 // 

function csv_to_array($input, $delimiter='|') 
 { 
     $header = null; 
     $data = array(); 
     $csvData = str_getcsv($input, "\n"); 

     foreach($csvData as $csvLine){ 
         if(is_null($header)) $header = explode($delimiter, $csvLine); 
         else{ 

             $items = explode($delimiter, $csvLine); 

             for($n = 0, $m = count($header); $n < $m; $n++){ 
                 $prepareData[$header[$n]] = $items[$n]; 
             } 

             $data[] = $prepareData; 
         } 
     } 

     return $data; 
 } 

//----------------------------------- 
 // 
 //Usage: 

$csvArr = csv_to_array(file_get_contents('test.csv')); 

?>

this code should help you a lot, but please save the file to CSV to use this function..and saving the file is not tough. and the source for the code is still the stated above link you just need to modify it as per your requirements..

HTH

Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77