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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

csv file issue

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
hi,
when excel converts an excel file to "csv" format if there is a "comma" within one of the rows before being converted then it puts "quotes" in that row. Now, i am using php to use that csv file and enter it into mysql database. but when i use explode("," $file) to break it apart i cannot avoid it breaking into more fields than required. Is there an easy solution to this ?
thanks
 
i guess the fgetcsv takes a file handle but i am not using a filehandle; i am receiving the file-contents as a string. are there any other methods that can do this??
 
Explode will not work for the cited reasons.
However, you must be reading the string somwhere.
How do you do that? If you tell us, we'll come up with a solution.
 
well,
i tried to use explode and as you said it does not work...i thought about using pregmatch along with explode but it seems to be not a reasonable solution.
$part = explode(",", $fcrow[$key]);
if(preg_match('/^"/',$fcrow[$part])){
$j = $part+1;
$enter = 0;
for ($i = $j; $i < count($part); $i++){
if(preg_match('/^"/',$fcrow[$j])){
$fcrow[$part] .= $fcrow[$part] . " " . $fcrow[$i];
$enter = 1;
}else if ($enter == 0){
while (!preg_match('/^"/',$fcrow[$j])){
$fcrow[$part] .= $fcrow[$part] . " " . $fcrow[$i];
}
}
}
}


this doesnt exactly work but --is a code in progress. i gues s what i want to do is explode by comma and then iterate through the loop and see if there is a double-quote (the reason i can do this is because the quotes appear only due to excel's).if i get a double quote then i want to go on till i get the second double quote(again- the above code only works for a pair of double quote).
i will keep on trying to work on this.
thanks
 
Take the string you recieve, write it to a temporary file, close the file and reopen it for read. Use fgetcsv() to read and parse the record.

Overkill and possibly slow, but it should work.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top