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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -