Re: Explode Function - Delimiter issue?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rid243
Forum Newbie
Posts: 5
Joined: Thu Oct 08, 2009 11:27 pm

Re: Explode Function - Delimiter issue?

Post 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!
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Explode Function - Delimiter issue?

Post by Mirge »

If you're on Windows, use \r\n... otherwise, \n...
pa28
Forum Newbie
Posts: 7
Joined: Mon Oct 05, 2009 9:26 pm

Re: Explode Function - Delimiter issue?

Post 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.
rid243
Forum Newbie
Posts: 5
Joined: Thu Oct 08, 2009 11:27 pm

Re: Explode Function - Delimiter issue?

Post by rid243 »

Thanks to both of you!

The

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

works perfectly!

Cheers
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Explode Function - Delimiter issue?

Post by requinix »

Have you heard of fgetcsv?
Post Reply