TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Some questions about file_get_contents (http://www.talkphp.com/general/4776-some-questions-about-file_get_contents.html)

Sam Granger 07-24-2009 05:33 PM

Some questions about file_get_contents
 
How do I manage to get the following, but with file_get_contents instead of fopen?

PHP Code:

$fp fopen($filename'r');
$content implode(''file($filename));
$content_array explode($seperator$content); 

I tried quite a few things, all without success.. :-/

My second question, I know file_get_contents is faster than fopen, reason why I'm using it. But is it more efficient for what I'm using fopen to do? I'm wanting to use this to convert a CSV into an array.

Salathe 07-24-2009 06:51 PM

Ok, a few points.
  1. The line with fopen() is doing absolutely nothing useful in that script. It is the call to the file() function that brings in the file, fopen isn't needed.
  2. To use file_get_contents to grab a file just use $content = file_get_contents($filename); along with that third line. That will do what the code snippet you showed does.
  3. You're wanting to parse CSV data please don't try and do it manually: PHP has a built-in function called fgetcsv or it is super-easy to use SplFileObject::fgetcsv (see the 2nd example in particular).

Sam Granger 07-24-2009 08:49 PM

Quote:

Originally Posted by Salathe (Post 27302)
Ok, a few points.
  1. The line with fopen() is doing absolutely nothing useful in that script. It is the call to the file() function that brings in the file, fopen isn't needed.
  2. To use file_get_contents to grab a file just use $content = file_get_contents($filename); along with that third line. That will do what the code snippet you showed does.
  3. You're wanting to parse CSV data please don't try and do it manually: PHP has a built-in function called fgetcsv or it is super-easy to use SplFileObject::fgetcsv (see the 2nd example in particular).

Salathe, that SplFileObject::fgetcsv looks pretty awesome! Thanks for the help, I'm going to give it a try now :-)

Salathe 07-24-2009 09:21 PM

It's certainly easier then the procedural way with fopen/while(fgetcsv)/fclose. SplFileObject has been available for yonks but not many people use it (that I've seen).

If the documentation doesn't make sense, blame me (I wrote it).

Good luck. 8-)

Sam Granger 07-24-2009 09:26 PM

Quote:

Originally Posted by Salathe (Post 27304)
It's certainly easier then the procedural way with fopen/while(fgetcsv)/fclose. SplFileObject has been available for yonks but not many people use it (that I've seen).

If the documentation doesn't make sense, blame me (I wrote it).

Good luck. 8-)

You seriously wrote that? It worked a charm! How fast is it compared to going down the file_get_contents & explode method?

I loved the fact that the function worked fine with eg:

Quote:

"crocodile","reptile","4"
"dolphin","mammal","0"
"duck","bird","2"
"koala","mammal","4"
"salmon","fish","0"
Not just
Quote:

crocodile,reptile,4
dolphin,mammal,0
duck,bird,2
koala,mammal,4
salmon,fish,0
Also loved this, nice and simple!
PHP Code:

$file->setCsvControl('|'); 


Salathe 07-24-2009 10:00 PM

Quote:

Originally Posted by Sam Granger (Post 27305)
You seriously wrote that?

I wrote the documentation, not the class. :-P

Quote:

Originally Posted by Sam Granger (Post 27305)
It worked a charm! How fast is it compared to going down the file_get_contents & explode method?

I don't know definitively but I'd guess slower since SplFileObject does a lot more than a quick file_get_contents/explode will. But I can't see it being a bottleneck and not something to worry about. The ability to foreach over the data and cater for field delimiters, escape characters, etc. simply wins it for me.


All times are GMT. The time now is 02:10 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0