RFC 4180 which deals with CSVs states the escape character is supposed to be a double quotation mark: (page 2)
7. If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"
str_getcsv
(No version information available, might be only in CVS)
str_getcsv — Analyse une chaîne de caractères CSV dans un tableau
Description
array str_getcsv
( string $input
[, string $delimiter
[, string $enclosure
[, string $escape
]]] )
Avertissement
Cette fonction n'est pas documentée et seule la liste des arguments est disponible.
str_getcsv
csv at rfc dot org
05-May-2008 01:15
05-May-2008 01:15
peev[dot]alexander at gmail dot com
20-Apr-2008 02:22
20-Apr-2008 02:22
CSV parsing and storage is not that hard to implement - see my example functions ( I believe they do a pretty good job - I use them in a production environment ):
<?php
if( !function_exists("parse_csv") ){
function parse_csv($string){
/* Author : Alexander Peev, posted at PHP.NET */
if( !function_exists("parse_csv_aux") ){
function parse_csv_aux( $string ){
$product = "";
$in_quote = FALSE;
$skipped_quote = FALSE;
for( $i = 0 ; $i < strlen($string) ; $i++ ){
if( $string{$i} == "\"" ){
if($in_quote){
if($skipped_quote){
$product .= "\"";
$skipped_quote = FALSE;
}
else if( !$skipped_quote ){
$skipped_quote = TRUE;
}
}
else{
if($skipped_quote) $skipped_quote = FALSE;
$in_quote = TRUE;
}
}
else if( $string{$i} == ";" ){
if($in_quote){
$product .= ";";
}
else{
$product .= " ; ";
}
}
else{
if($in_quote){
$in_quote = FALSE;
$product .= $string{$i};
}
else{
$product .= $string{$i};
}
}
}
return $product;
}
}
$data = array();
if( is_string($string) && ( stripos($string, "\n") !== FALSE ) ){
$data = explode("\n", parse_csv_aux($string) );
foreach($data as $key => $row){
$columns = array();
//$row = strtr( $row, array( "\";\"" => "\";\"", ";" => " ; " ) );
if( stripos($row, " ; ") !== FALSE ){
$columns = explode( " ; ", $row );
if( !is_array($columns) )$columns = array( strval($columns) );
$data[$key] = $columns;
}
}
return $data;
}
else if( is_string($string) && ( stripos( ($string = parse_csv_aux($string)), " ; ") !== FALSE ) ){
$columns = explode( " ; ", $string );
if( !is_array($columns) )$columns = array( strval($columns) );
return array($columns);
}
else return strval($string);
} /* end function parse_csv */
} /* end not function exists parse_csv */
if( !function_exists("store_csv") ){
function store_csv($data){
/* Author : Alexander Peev, posted at PHP.NET */
if( !function_exists("store_csv_aux") ){
function store_csv_aux( $string ){
$string = strtr( $string, array( "\n" => "" ) );
$product = "";
$in_quote = FALSE;
for( $i = 0 ; $i < strlen($string) ; $i++ ){
if( $string{$i} == "\"" ){
if($in_quote){
$product .= "\"\"";
}
else{
$product .= "\"\"\"";
$in_quote = TRUE;
}
}
else if( $string{$i} == ";" ){
if($in_quote){
$product .= ";";
}
else{
$product .= "\";";
$in_quote = TRUE;
}
}
else{
if($in_quote){
$product .= "\"";
$in_quote = FALSE;
$product .= $string{$i};
}
else{
$product .= $string{$i};
}
}
}
if($in_quote)$product .= "\"";
return $product;
}
}
if(!is_array($data))return strval($data);
$passed_rows = FALSE;
$product = "";
foreach($data as $row){
if( $passed_rows )$product .= "\n";
if( is_array($row) ){
$columns = "";
$passed_cols = FALSE;
foreach($row as $column){
if( $passed_cols )$columns .= ";";
$columns .= store_csv_aux( $column );
$passed_cols =TRUE;
}
$product .= strval($columns);
}
else{
$product .= strtr( strval($row), array("\n" => "") );
}
$passed_rows = TRUE;
}
return $product;
} /* end function store_csv */
} /* end not function exists store_csv */
?>
jon at webignition dot net
04-Apr-2008 03:52
04-Apr-2008 03:52
Parameter documentation, currently missing here, can be found at:
> http://www.php.net/manual/en/function.fgetcsv.php
justin at cam dot org
15-Feb-2007 10:25
15-Feb-2007 10:25
There's a discussion of how to perform this task in the user notes for the split() function.
http://www.php.net/manual/en/function.split.php
