stevepatd Posted January 3, 2011 Share Posted January 3, 2011 I'm trying to parse a csv file. The user sometimes enters commas into a field in Excel. Excel will then put quotes around that field that has commas in it when creating the csv file. Now my fields are all off because of these user entries with commas. I tried using an fgetcsv thinking it may handle this situation but I get an error with an "Array to string conversion" . How do I manipulate these lines that have commas within quotes in a csv file? $student = trim(fgets($fh)); $line = explode(",",$student); Link to comment https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2011 Share Posted January 3, 2011 You should use fgetcsv and if you want help with the error you got when using it, you would need to post your code and the error message. Link to comment https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/#findComment-1154391 Share on other sites More sharing options...
stevepatd Posted January 3, 2011 Author Share Posted January 3, 2011 When I use the fgetcsv I get this. - thanks for the help. $fh = fopen("file.csv",'r'); $student = trim(fgetcsv($fh)); Error message is Notice: Array to string conversion on my second line. Link to comment https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/#findComment-1154401 Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 Does the notice really say "on my second line"??? I doubt the error is caused by either of those lines posted. it really helps if we can see all pertinent code and especially the lines up to and including "my second line". Link to comment https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/#findComment-1154403 Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 on the other hand, i'm probably wrong. you can't use trim() on an array. $student = trim(fgetcsv($fh)); // No! Link to comment https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/#findComment-1154404 Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 example of fgetcsv from manual: $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); Link to comment https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/#findComment-1154406 Share on other sites More sharing options...
jcbones Posted January 4, 2011 Share Posted January 4, 2011 if you have to trim the results, you can use array_map(). $student = array_map('trim',fgetcsv($fh)); Link to comment https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/#findComment-1154457 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.