Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any examples of the mysterious str_getcsv function? 2

Status
Not open for further replies.

sen5241b

IS-IT--Management
Sep 27, 2007
199
US
The manual says no documentation is available. ???

Anyone have an example? I can't use fgetcsv cause my file is encrypted. I read in the file, decrypt it and then I need to get the CSV contents into an array. Can str_getcsv help?
 
There's not much about it in PHP.net's online manual, other than its parameter list. No mention of what version of PHP actually supports it.

However it should not be to difficult come up with a function that can "explode" a long CSV string with several lines into a bi-dimensional array.

something like this should be a start
Code:
function csvstr_arr($string, $delimiter){

$lines=explode("\r\n",$string);
if(!$lines){
return "string contains less than two rows";
}
else{
$csvarray=array();


if(!strpos($string,$delimiter)){
return "Delmiter not found in string";
}

foreach($lines as $line){
$csvarray[]=explode($delimiter,$line);
}

return $csvarray;
}

}




$string="onevalue,2values,three values \r\n row2value1,row2value2,row2value3 \r\n row3value1, row3value2, row3value3";

echo "<pre>";
print_r(csvstr_arr($string,","));
echo "</pre>";

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
the arguments are the same as for fgetcsv, save that the first argument should a string rather than a file pointer.

with these new functions that are incompletely documented, you can always take a look at the source code.
 
str_getcsv generates an error.

A big problem I've had is that a text field with embedded spaces, in a CSV file, is enclosed by double quotes. This is standard syntax for a CSV.

After decrypting my CSV file contents I try to 'explode' it but the double quotes cause problems. Is there some function that will escape all double quotes before I try to explode it?
 
why would you need to?

what error do you get with str_getcsv?
 
Code:
<?php
$thestr = "\"fred\", 4, 89, \"hey there\"";
$displayit = array();		
$displayit = str_getcsv($thestr);
echo ' <BR>';
print_r($displayit);
	?>

Produces: Fatal error: Call to undefined function str_getcsv() in /home/content/s/c/o/scottlmoore111/html/ObsceneClean/words.php on line 4
 
then you are not using an up to date version of php.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top