str_getcsv
PARSES a CSV STRING into an ARRAY.
A CSV, comma-separated values file is a delimited text file that uses a comma to separate values.
Using the $escape as an empty STRING disables the proprietary escape mechanism.
Usually an $enclosure character is escaped inside a field by doubling it; however, the $escape character can be used as an alternative.
It says that empty and backslach has the same meaning when used as default value for either $escape or $enclosure.
Other than allowing to escape the enclosure character the escape character has no special meaning; it isn't even meant to escape itself.
This function take in account the LOCALE settings.
<?php
arr str_getcsv ( str $input [,
str $delimiter = "," [,
str $enclosure = '"' [,
str $escape = "\\" ]]] )
where,
$input = The input STRING to parse
$delimiter = To set the field delimiter character
$enclosure = To set the field enclosure character
$escape = To set the escape character
?>
$input
The input STRING to parse.
$delimiter
The field delimiter character.
Only one character is accepted.
$enclosure
The field enclosure character.
Only one character is accepted.
$escape
The escape character.
At most one character is accepted.
EXERCISE
<?php
$strcsv01 = 'John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",
SomeTown,SD, 91234, Blankman,,SomeTown, SD, 00298
"João ""the brazilian"", Anne",Jet,"9th,
at Terrace plc",Desert City,CO,00123';
$getcsv01 = str_getcsv($strcsv01);
// print_r($getcsv01);
foreach($getcsv01 as $gcsv01)
{
echo $gcsv01 . '<br>';
}
?>
EXERCISE
<?php
$strcsv02 = 'John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",
SomeTown,SD, 91234, Blankman,,SomeTown, SD, 00298
"João ""the brazilian"", Anne",Jet,"9th,
at Terrace plc",Desert City,CO,00123';
$delim02 = '"';
$encls02 = '"';
$scape02 = '<br>';
$getcsv02 = str_getcsv($strcsv02,
$delim02,
$encls02,
$scape02);
foreach($getcsv02 as $gcsv02)
{
echo $gcsv02 . '<br>';
}
?>
EXERCISE
<?php
echo "<br>- - - - - - - -<br>";
function strcsv1($par1)
{
echo "<br>str_getcsv(\"$par1\")<br>";
var_dump(str_getcsv($par1));
echo "<br>- - - - - - - -<br>";
}
function strcsv2($par1, $par2)
{
echo "<br>str_getcsv(\"$par1\", \"$par2\")<br>";
var_dump(str_getcsv($par1, $par2));
echo "<br>- - - - - - - -<br>";
}
function strcsv3($par1, $par2, $par3)
{
echo "<br>str_getcsv(\"$par1\", \"$par2\", \"$par3\")<br>";
var_dump(str_getcsv($par1, $par2, $par3));
echo "<br>- - - - - - - -<br>";
}
function strcsv4($par1, $par2, $par3, $par4)
{
echo "<br>str_getcsv(\"$par1\", \"$par2\", \"$par3\", \"$par4\")<br>";
var_dump(str_getcsv($par1, $par2, $par3, $par4));
echo "<br>- - - - - - - -<br>";
}
strcsv1('"f", "o", ""');
strcsv2('foo||bar', '|');
strcsv2('foo|bar', '|');
strcsv3('|foo|-|bar|', '-', '|');
strcsv4('|f.|.|bar|.|-|-.|', '.', '|', '-');
strcsv4('.foo..bar.', '.', '.', '.');
strcsv4('.foo. .bar.', ' ', '.', '.');
strcsv4('1foo1 1bar111', ' ', '1 ', '\ ');
strcsv4('.foo . . bar .', ' ', '.', '');
strcsv2('" "" "', ' ');
strcsv1(NULL);
strcsv1('');
?>
EXERCISE
<?php
$contents = <<<EOS
"cell1","cell2\\","cell3","cell4"
EOS;
print_r(str_getcsv($contents, ',', '"', ''));
?>