TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-24-2009, 05:33 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default 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.
Sam Granger is offline  
Reply With Quote
Old 07-24-2009, 06:51 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 07-24-2009, 08:49 PM   #3 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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
Sam Granger is offline  
Reply With Quote
Old 07-24-2009, 09:21 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 07-24-2009, 09:26 PM   #5 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
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('|'); 
Sam Granger is offline  
Reply With Quote
Old 07-24-2009, 10:00 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Sam Granger View Post
You seriously wrote that?
I wrote the documentation, not the class.

Quote:
Originally Posted by Sam Granger View Post
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.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Several Questions - Security and Performance Killswitch General 0 12-09-2008 03:10 AM
4 basic questions Yoosha Absolute Beginners 5 09-06-2008 03:38 PM
Zend Framework - questions questions abiko Advanced PHP Programming 4 02-29-2008 12:46 AM
Dozens of questions which I cannot find. v_v Orc MySQL & Databases 11 01-31-2008 07:50 PM
A Few AJAX Questions Gurnk Javascript, AJAX, E4X 4 10-23-2007 11:30 AM


All times are GMT. The time now is 01:11 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design