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); Quote 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. Quote 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. Quote 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". Quote 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! Quote 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); Quote 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)); Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.