Obama '08

               
   

Go Back   Mike Simonds > Salesforce > PHP Snippets

This is a discussion on php csv parse function within the PHP Snippets forums, part of the Salesforce category; This php function parses a CSV file and returns the data in

Reply
 
LinkBack Thread Tools Rate Thread
  #1  
Old 05-23-2008, 09:32 AM
Administrator
 
Join Date: May 2007
Posts: 246
Send a message via AIM to mike Send a message via MSN to mike Send a message via Yahoo to mike Send a message via Skype™ to mike
php csv parse function

This php function parses a CSV file and returns the data in a multidimensional array. I use it in many of my scripts and it works perfectly on all CSV files. So if you need a PHP CSV function that is simple and here for anyone to take, please use it.

I will post an example of its use later

PHP Code:
function importcsv($file$head false$delim ","$len 1000)
{

    
$return false;
    
$handle fopen($file"r");
    if (
$head)
    {
        
$header fgetcsv($handle$len$delim);
    }
    while ((
$data fgetcsv($handle$len$delim)) !== false)
    {
        if (
$head and isset($header))
        {
            foreach (
$header as $key => $heading)
            {
                
$row[$heading] = (isset($data[$key])) ? $data[$key] : '';
            }
            
$return[] = $row;
        }
        else
        {
            
$return[] = $data;
        }
    }
    
fclose($handle);
    return 
$return;

Now I do not take credit for writing this function, I found it somewhere. I cannot remember where I did and would give the developer full credit if I knew.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2  
Old 05-23-2008, 10:53 AM
Administrator
 
Join Date: May 2007
Posts: 246
Send a message via AIM to mike Send a message via MSN to mike Send a message via Yahoo to mike Send a message via Skype™ to mike

So let's say that your CSV file contents looked like this:

Code:
"ID","USERNAME","LASTNAME"
"00540000000wxjkAAA","heather@sportsrant.com","Simonds"
"00540000000wvEyAAI","simonds@sportsrant.com","Simonds"
So to test this, you could write a simple foreach () loop after passing this function your CSV file

PHP Code:
$file 'extract.csv';

$data importcsv($file$head true);
foreach (
$data as $line)
{
    echo 
'<pre>' print_r($linetrue) . '</pre>';
}

exit; 
and your output would be something like this:

Code:
Array
(
    [ID] => 00540000000wxjkAAA
    [USERNAME] => heather@sportsrant.com
    [LASTNAME] => Simonds
)
Array
(
    [ID] => 00540000000wvEyAAI
    [USERNAME] => simonds@sportsrant.com
    [LASTNAME] => Simonds
)

This is really a great PHP snippet for parsing CSV files and extremely easy to use
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 05-24-2008, 12:51 AM
Junior Member
 
Join Date: May 2008
Posts: 7

Mike, I am just finishing up a project where I implemented a CSV importer that takes data from Eventbrite.com and imports it into custom objects in Salesforce. It's pretty slick and I used this function.

One important thing to note about it. It's not UTF-8 compliant. If you have double-byte characters in the data, it can cause problems. I Googled around for fixes and there were some suggestions about setting a locale and the like, but nothing worked.

It's a great function, but that's a caveat to it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 05-24-2008, 04:06 PM
Administrator
 
Join Date: May 2007
Posts: 246
Send a message via AIM to mike Send a message via MSN to mike Send a message via Yahoo to mike Send a message via Skype™ to mike

Yeah Scott I did notice that problem too when I was writing a series of scripts that process a system like you built kind of like that. I found a way around that though:


PHP Code:
foreach ($data as $key => $value)
{
    
$data[$key] = utf8_encode($value);

I am sure that you already know of the utf8_encode function, but I was getting API errors back from salesforce on some of my inserts and added this and the errors went away

I am also going to email you a little script that may help you out

~Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



Powered by vBulletin


SEO by vBSEO 3.2.0 RC8 ©2008, Crawlability, Inc.

1 2 3 4 5