Brian Johnson
Brian Johnson

Reputation: 131

PHP: How can I eliminate quotes around output from CSV file?

This code:

<?php $curl=curl_init();
curl_setopt ($curl,CURLOPT_URL,"http://download.finance.yahoo.com/d/quotes.csv?s=XIN&f=l1c1p2rj1y&e=.csv");
curl_setopt ($curl,CURLOPT_HEADER,0);
ob_start();
curl_exec ($curl);
curl_close ($curl);
$data=ob_get_clean();
$data = explode(",",$data);
foreach ($data as $results)
echo "<td>$results</td>";
?>

yields these results in my browser: 2.80 +0.02 "+0.72%" 1.85 204.2M 1.44

How can I have this PHP code above eliminate the quotations around the "+0.72%" so the end result is just: 0.72% ?

Upvotes: 1

Views: 245

Answers (1)

poncha
poncha

Reputation: 7856

Use fopen and fgetcsv to read the csv data, instead of exploding the lines yourself

EDIT:

In case you're dealing with a string that you already obtained with curl, You can parse a line of csv data by using str_getcsv like this:

$values = str_getcsv($line);

Note that it only works on a single line of input, so if your input has multiple lines, you need to explode it by newline first...

Upvotes: 5

Related Questions