file_get_contents csv

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
flower
Forum Newbie
Posts: 6
Joined: Tue Aug 25, 2009 5:10 pm

file_get_contents csv

Post by flower »

I want to read a file from another website by using the function file_get_contents.
The file is a .txt file with csv data, the data is separated by semicolons.
When I open the file with notepad, everything is normal, but when I print it to the screen with php, after it has been read by file_get_contents, the semicolons are gone.
The file:
http://www.euronext.com/search/download ... e=dd/MM/yy

Thanks in advance,
Flower.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: file_get_contents csv

Post by Mark Baker »

I know I need to use glasses for reading and screen work, and I'm not wearing them at the moment; but I can see a lot of semi-colons in that file, exactly where I'd expect to see them
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file_get_contents csv

Post by requinix »

I think s/he's saying that the semicolons are gone after the PHP works on the file.

Post code.
flower
Forum Newbie
Posts: 6
Joined: Tue Aug 25, 2009 5:10 pm

Re: file_get_contents csv

Post by flower »

Thanks for the quick response.

Code: Select all

 
<?php
 
if(file_get_contents('http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&belongsToList=market_EURLS&lan=NL&mep=8626&resultsTitle=Amsterdam+-+Euronext&cha=1800&totalOfInstruments=195&format=txt&formatDecimal=,&formatDate=dd/MM/yy')) {
    $contents = file_get_contents('http://www.euronext.com/search/download/trapridownloadpopup.jcsv?pricesearchresults=actif&filter=1&belongsToList=market_EURLS&lan=NL&mep=8626&resultsTitle=Amsterdam+-+Euronext&cha=1800&totalOfInstruments=195&format=xls&formatDecimal=,&formatDate=dd/MM/yy');
    $contents_array = explode("\n",$contents);
    $i = 4;
    foreach($contents_array as $value) {
        if($i >= 4) {
            list($company,$ISIN,$Euronext_code,$Market,$Symbol,$ICB_Sector_Level_4,$Handelsvaluta,$Laatst,$Aantal,$percentage,$Datum_tijd,$Omzet,$Totaal_aantal_aandelen,$Capitalisation,$Trading_mode,$Dag_Open,$Dag_Hoog,$Dag_Hoog_Datum_tijd,$Dag_Laag,$Dag_Laag_Datum_tijd) = explode(" ",$value);
        }
        //echo '----'.$company.'----';
        if(strlen($company) > 0) {
            
            $data[$company] = 
            array(
            "ISIN"                  => $ISIN, 
            "Euronext_code"         => $Euronext_code, 
            "Market"                => $Market, 
            "Symbol"                => $Symbol, 
            "ICB_Sector_Level_4"    => $ICB_Sector_Level_4, 
            "Handelsvaluta"         => $Handelsvaluta, 
            "Laatst"                => $Laatst, 
            "Aantal"                => $Aantal, 
            "Percentage"            => $percentage, 
            "Datetime"              => $Datum_tijd,
            "Omzet"                 => $Omzet,
            "Tot aandelen"          => $Totaal_aantal_aandelen,
            "Capitalisation"        => $Capitalisation,
            "Trading_mode"          => $Trading_mode,
            "Dag_Open"              => $Dag_Open,
            "Dag_Hoog"              => $Dag_Hoog,
            "Dag_Hoog_datetime"     => $Dag_Hoog_Datum_tijd,
            "Dag_Laag"              => $Dag_Laag,
            "Dag_Laag_Datum_tijd"   => $Dag_Laag_Datum_tijd
            );
        }
        $i++;
    }
}
 
?>
 
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: file_get_contents csv

Post by alex.barylski »

When you print to screen the semi-colons are gone? Maybe the browser is interpreting them...dump the contents to a local file and open that file in notepad, are the semi-colons gone?
flower
Forum Newbie
Posts: 6
Joined: Tue Aug 25, 2009 5:10 pm

Re: file_get_contents csv

Post by flower »

Glad you ask..
I already tried, the semicolons are still nowhere to be found.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file_get_contents csv

Post by requinix »

*cough*

There are no semicolons in the CSV. Anywhere.

Check your URLs more carefully ;)
flower
Forum Newbie
Posts: 6
Joined: Tue Aug 25, 2009 5:10 pm

Re: file_get_contents csv

Post by flower »

I'm Dutch but when I say semi-colon I mean this thing ;
When I open the link, I posted earlier, in Mozilla Firefox, It asks me to download composition.txt. When I open that file, I see the following:

http://img195.imageshack.us/img195/9396/semicolons.png

It's full of semi-colons, or am I wrong?
yasir_memon
Forum Commoner
Posts: 48
Joined: Tue Aug 11, 2009 12:29 am

Re: file_get_contents csv

Post by yasir_memon »

flower
Forum Newbie
Posts: 6
Joined: Tue Aug 25, 2009 5:10 pm

Re: file_get_contents csv

Post by flower »

I don't get it, that's the same link?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: file_get_contents csv

Post by Eran »

why aren't you using fgetcsv()?
flower
Forum Newbie
Posts: 6
Joined: Tue Aug 25, 2009 5:10 pm

Re: file_get_contents csv

Post by flower »

Thanks for your reply.
I didn't know fgetcsv() existed. I'm going to try it right now.

Edit:
Thanks, fgetcsv() works perfectly till now!
Post Reply