Jump to content
Larry Ullman's Book Forums

Need Function For Turning Csv List Into An Array


Recommended Posts

I was using " str_getcsv($keyDescriptor, ",") " locally and it working fine. When I put it on a remote server I get an error that the function is not defined.

 

Is there another function that will do the same thing? Or do I have to write one myself using a regex?

 

thanks,

chop

Link to comment
Share on other sites

Okay, I found this and tried it. It almost works but the first and the last character of the csv has been chopped off.

 

function csv2array($input,$delimiter=',',$enclosure='',$escape='\\'){

$fields=explode($enclosure.$delimiter.$enclosure,substr($input,1,-1));

foreach ($fields as $key=>$value)

$fields[$key]=str_replace($escape.$enclosure,$enclosure,$value);

return($fields);

}

 

// I eliminated the single quote enclosure argument in the above example

 

Any clue on what might be causing the truncation?

Link to comment
Share on other sites

I found that if I make this change:

 

substr($input,0,-1) from the original substr($input,1,-1)

 

it only chops off the very last character.. thought I had it but I've tried numerous combination in a hit-or-miss fashion and I can't get that last character. I could cheat and save the last char. before sending it to the function -but that's cheating :)

Link to comment
Share on other sites

Probably I'm just confused, but won't the explode function suffice? For example, if you have the following CSV data:

 

1,27,Billy Bob,7986,Yeah!

 

And then assign that to a variable as a string as follows:

 

$CSVData = '1,27,Billy Bob,7986,Yeah!';

 

You can then run the explode function on it like follows, right?

 

$CSVDataArray = explode(',', $CSVData);

 

Now $CSVDataArray will contain the following:

 

$CSVDataArray[0] = '1'
$CSVDataArray[1] = '27'
$CSVDataArray[2] = 'Billy Bob'
$CSVDataArray[3] = '7986'
$CSVDataArray[4] = 'Yeah!'

 

Is that what you want?

Link to comment
Share on other sites

Yes, right again HartleySan. I went to the php.net site to look for code to replace the unavailable "str_getcsv()" and there were many scripts like the one I was using (and not working correctly). So, I figured it couldn't be that simple and didn't know about explode. The posts are fairly recent and there's no use of explode. My only guess is that the ones provided are supposed to do more than the simple explode. If you want to take a look at what I'm talking about, here's the link. I'd like to understand it.

 

http://www.php.net/manual/en/function.str-getcsv.php

 

thanks for the simple solution

 

chop

Link to comment
Share on other sites

The str_getcsv function really isn't anything fancy. It would appear that is goes one step beyond a simple explode, and takes into account escaping double quotation marks, since you may have string data that contains double quotation marks, but is also surrounded by double quotation marks (because the data is a set of strings).

 

I liked the top example from the following page: http://php.net/manual/en/function.str-getcsv.php

 

For people who haven't got access to str_getcsv() in their version of PHP and want to convert strings from CSV, I have seen many huge swaths of code and regexes to do this extremely simple task. Here's minimal, fast code:

 

<?php

function csv2array($input,$delimiter=',',$enclosure='"',$escape='\\'){

$fields=explode($enclosure.$delimiter.$enclosure,substr($input,1,-1));

foreach ($fields as $key=>$value)

$fields[$key]=str_replace($escape.$enclosure,$enclosure,$value);

return($fields);

}

 

function array2csv($input,$delimiter=',',$enclosure='"',$escape='\\'){

foreach ($input as $key=>$value)

$input[$key]=str_replace($enclosure,$escape.$enclosure,$value);

return $enclosure.implode($enclosure.$delimiter.$enclosure,$input).$enclosure;

}

 

$data=array("one","\"two\"");

for ($i=0;$i<100;$i++) $data=array(array2csv($data),$i);

echo "Encoded $i times...".var_export($data,true)."\n";

for ($j=0;$j<$i;$j++) $data=csv2array($data[0]);

echo "Decoded $i times...".var_export($data,true)."\n";

?>

 

Pretty straightforward. ;-)

 

Yes, I intentionally changed the function names,

Link to comment
Share on other sites

Yeah, I figured as much. In my book, simpler is always better. And I must confess, I have no experience working with CSV data, but as far as I know, it's just data separated by commas. I was just assuming as much when I made my suggestion.

 

Glad it worked though.

Link to comment
Share on other sites

 Share

×
×
  • Create New...