Page 1 of 1

Re: Explode Function - Delimiter issue?

Posted: Thu Oct 08, 2009 11:37 pm
by rid243
Hi all,

I'm trying to use PHP to read this:
http://download.finance.yahoo.com/d/quo ... &f=nl1c1&e

The output is this:
"GEN ELECTRIC CO",16.22,+0.06
"Google Inc.",514.18,-3.36

I'm trying to use the explode function to seperate the top from bottom but it keeps bundling them together when I use this:

$csv = @file_get_contents($url);
$quote = explode("\r", $csv) ;

So if I echo $quote[0] it looks like this: "GEN ELECTRIC CO",16.22,+0.06 "Google Inc.",514.18,-3.36

In actual fact, I want

$quote[0] = "GEN ELECTRIC CO",16.22,+0.06
$quote[1] = "Google Inc.",514.18,-3.36

Thanks!

Re: Explode Function - Delimiter issue?

Posted: Thu Oct 08, 2009 11:44 pm
by Mirge
If you're on Windows, use \r\n... otherwise, \n...

Re: Explode Function - Delimiter issue?

Posted: Fri Oct 09, 2009 12:01 am
by pa28
Mirge wrote:If you're on Windows, use \r\n... otherwise, \n...
Using \n in the explode() function will be compatible with both Windows and non-Windows systems. Then use trim($quote[0]) etc. to remove the redundant \r, if present.

EDIT:
On second thoughts, you could do away with the file_get_contents() and just do:

Code: Select all

$quote = @file($url, FILE_IGNORE_NEW_LINES);
That will deal with the newlines for you, and produces an array with each line in a new element.

Re: Explode Function - Delimiter issue?

Posted: Fri Oct 09, 2009 12:17 am
by rid243
Thanks to both of you!

The

$quote = @file($url, FILE_IGNORE_NEW_LINES);

works perfectly!

Cheers

Re: Explode Function - Delimiter issue?

Posted: Fri Oct 09, 2009 12:48 am
by requinix
Have you heard of fgetcsv?