note 88137 modified in function.str-replace by danbrown
| From: | danbrown@php.net | Date: | Mon, 12 Jan 2009 16:32:52 +0000 |
| Subject: | note 88137 modified in function.str-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149101@lists.php.net to get a copy of this message | ||
Came across a difference between Excel 2003 vs Excel 2007 documents and the different formats they
push out csv files.
In 2007, the format pushes out the chr(13) instead of the \n in 2003.
Just as a safety precaution, do a quick replace on the fread contents from the excel file :-)
<?php
$lineseparator = "\n";
// check for the user supplied list type format
if ($_REQUEST['listtype'] == "csv")
$fieldseparator = ",";
elseif ($_REQUEST["listtype"] == "tab")
$fieldseparator = "\t";
// replaces the carriage return with the \n character
$csvcontent = str_replace(chr(13), $lineseparator,$csvcontent);
// then
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
if ($lines >1) { // skips header row
$line = trim($line," \t");
$line = str_replace("\r","",$line);
// split the line up
$linearray = explode($fieldseparator,$line);
// take care of The "Billy" the kid issue
for ($x = 0; $x<sizeof($linearray); $x++) {
$linearray[$x] = str_replace('""','^',$linearray[$x]);
$linearray[$x] = str_replace('""','^',$linearray[$x]);
$linearray[$x] = str_replace('"','',$linearray[$x]);
$linearray[$x] = str_replace('"','',$linearray[$x]);
$linearray[$x] = str_replace('^','"',$linearray[$x]);
}
// then reference each element in order of the line by array and key
// echo $linearray[0]; //, etc.
}
}
?>
Happy Coding!
--was--
Came across a difference between Excel 2003 vs Excel 2007 documents and the different formats they
push out csv files.
In 2007, the format pushes out the chr(13) instead of the \n in 2003.
Just as a safety precaution, do a quick replace on the fread contents from the excel file :-)
$lineseparator = "\n";
// check for the user supplied list type format
if ($_REQUEST['listtype'] == "csv")
$fieldseparator = ",";
elseif ($_REQUEST["listtype"] == "tab")
$fieldseparator = "\t";
// replaces the carriage return with the \n character
$csvcontent = str_replace(chr(13), $lineseparator,$csvcontent);
// then
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
if ($lines >1) { // skips header row
$line = trim($line," \t");
$line = str_replace("\r","",$line);
// split the line up
$linearray = explode($fieldseparator,$line);
// take care of The "Billy" the kid issue
for ($x = 0; $x<sizeof($linearray); $x++) {
$linearray[$x] = str_replace('""','^',$linearray[$x]);
$linearray[$x] = str_replace('""','^',$linearray[$x]);
$linearray[$x] = str_replace('"','',$linearray[$x]);
$linearray[$x] = str_replace('"','',$linearray[$x]);
$linearray[$x] = str_replace('^','"',$linearray[$x]);
}
// then reference each element in order of the line by array and key
// echo $linearray[0]; //, etc.
}
}
Happy Coding!
http://php.net/manual/en/function.str-replace.php