php - Loading CSV from remote file -
i writing following code csv stock data, when have downloaded string splitting in following way
<company name>,<stock price>,<stock change> <company2 name>,<stock price2>,<stock change2>
i have tried split array using /n character using php function explode. did not split properly. here code:
public function getquotes() { $result = array(); $format = $this->format; $stockstring = ""; foreach ($this->stocks $stock) { $stockstring = $stockstring . $stock . "+"; } //remove last "+" $stockstring = substr($stockstring,0,strlen($stockstring)-1); $s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=". $stockstring . "&f=" . $format . "&e=.csv"); //the splitting done here. } }
thank in advance
use file
function instead of file_get_contents
- split content you, php manual says:
returns file in array. each element of array corresponds line in file, newline still attached. upon failure, file() returns false.
then can use str_getcsv each element of array field values.
public function getquotes() { $result = array(); $format = $this->format; $stockstring = ""; foreach ($this->stocks $stock) { $stockstring = $stockstring . $stock . "+"; } //remove last "+" $stockstring = substr($stockstring,0,strlen($stockstring)-1); $s = file("http://finance.yahoo.com/d/quotes.csv?s=". $stockstring . "&f=" . $format . "&e=.csv"); foreach($s $line) { $values = str_getcsv($line); } }
Comments
Post a Comment