note 41483 modified in function.fgetcsv by aidan
| From: | aidan@php.net | Date: | Thu, 01 Jul 2004 00:58:59 +0000 |
| Subject: | note 41483 modified in function.fgetcsv by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72206@lists.php.net to get a copy of this message | ||
The following modification will hide the unnecessary delimiter in the array
that is returned when fgetcsvfromline is called.
<?php
function fgetcsvfromline ($line, $columnCount, $delimiterChar = ',',
$enclosureChar = '"') {
global $regExpSpecialChars;
$matches = array();
$delimiterChar = strtr($delimiterChar, $regExpSpecialChars);
$enclosureChar = strtr($enclosureChar, $regExpSpecialChars);
$cutpoint = strlen($delimiterChar)+1;
$regExp = "/^";
for ($i = 0; $i < $columnCount; $i++) {
$regExp .=
$enclosureChar.'?(.*?)'.$enclosureChar.'?'.$delimiterChar;
}
$regExp = substr($regExp,0,-$cutpoint).'/';
if (preg_match($regExp, $line, $matches)) {
return $matches;
}
return 0;
}
?>
=== If you were getting
[0] =>
"Ma"rk","Bergeron","rocks","12345,"times"
[1] => "
[2] => Ma"rk
[3] => "
..etc
You will now get
[0] =>
"Ma"rk","Bergeron","rocks","12345,"times"
[1] => Ma"rk
...etc
--was--
The following modification will hide the unnecessary delimiter in the array
that is returned when fgetcsvfromline is called.
function fgetcsvfromline ($line, $columnCount, $delimiterChar = ',',
$enclosureChar = '"') {
global $regExpSpecialChars;
$matches = array();
$delimiterChar = strtr($delimiterChar, $regExpSpecialChars);
$enclosureChar = strtr($enclosureChar, $regExpSpecialChars);
$cutpoint = strlen($delimiterChar)+1;
$regExp = "/^";
for ($i = 0; $i < $columnCount; $i++) {
$regExp .=
$enclosureChar.'?(.*?)'.$enclosureChar.'?'.$delimiterChar;
}
$regExp = substr($regExp,0,-$cutpoint).'/';
if (preg_match($regExp, $line, $matches)) {
return $matches;
}
return 0;
}
=== If you were getting
[0] =>
"Ma"rk","Bergeron","rocks","12345,"times"
[1] => "
[2] => Ma"rk
[3] => "
..etc
You will now get
[0] =>
"Ma"rk","Bergeron","rocks","12345,"times"
[1] => Ma"rk
...etc
http://php.net/manual/en/function.fgetcsv.php