weep Posted April 27, 2011 Share Posted April 27, 2011 Greetings! I have a CSV file that looks something like this: ,,,,,,,,sdfsdf sdfsdf,,,,,,,,,,,,,,,,,,,,,,sdfsdfsdf:,,,,,,,,2011-04-27,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,dfgdfg,,fgh,,,,,gfh,,,,,,,,,,,,,,fghfgh,,,,,fghf,, fghfh,, fgh,,,,,,fhfgh,,,, <?php $handle = fopen("csv.csv", "r"); while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) { echo "<pre>"; print_r($data); echo "<pre>"; } ?> Now this prints everything, could someone please provide me with an example of how I can ignore the "empty fields"? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2011 Share Posted April 27, 2011 Define: ignore the "empty fields"? What do you want to do when there is an empty field? What end result are you trying to achieve (because I doubt using print_r() on your data is the final result.) Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206870 Share on other sites More sharing options...
weep Posted April 27, 2011 Author Share Posted April 27, 2011 Sorry for not being clear, sometimes I forget that people cant read minds I don't intend to use the given code (at least not the way it is now), it was just out of the blue. What I am looking for is a way to assign available values to an array and ignore the "empty fields". Something like: $data[0] => "sdfsdf sdfsdf" $data[1] => "sdfsdfsdf:" $data[2] => "2011-04-27" and so on... ps. Come to think of one more issue I most certainly will need help with, so I may as well ask now: What do I do if there is a comma inside a sentence that I want to assign to a variable? I.e; ",,,,Hello, my name is blah,,,," would give me: $data[0] => "Hello" $data[1] => "my name is blah" Though I would like: $data[0] => "Hello, my name is blah" Cheers Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206882 Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2011 Share Posted April 27, 2011 The fields making up a CSV generally have significance. If you just get the non-empty values, without regard to the position they came from in the CSV, you loose the meaning of that data. A string would be enclosed in quotes if it contains a comma that is part of the data and not a data separator or all string data values could be enclosed in quotes. Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206887 Share on other sites More sharing options...
salathe Posted April 27, 2011 Share Posted April 27, 2011 You could use array_filter() to remove the empty values (precisely what you use will vary depending on what you want to filter in/out). $array = array('', 'cake', 'pie', '', '', ''); $filtered = array_filter($array, 'strlen'); // $filtered is now array('cake', 'pie') (You might also like to use array_values() on the filtered array, to get back sequential 0-based keys.) As for commas within strings, for obvious reasons, that is not allowed unless the field is wrapped in double-quotes. cake,"apple, banana and pears" <--- OK, 2 fields cake,apple, banana and pears <--- SAD FACE, 3 fields Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206897 Share on other sites More sharing options...
weep Posted April 27, 2011 Author Share Posted April 27, 2011 As long as I know which line they are on (I should be able to count them as I work my way though the file, right?) then I should be fine. The idea is to reconstruct an excel file, goin through csv. Excel 1 -> CSV -> Excel 2 Excel 1 is a mess... Should be enclosed in quotes you say, but they are not... Here is a sample how it really looks: 2011-03-12,,,,,John Doe,,,,,,,,,,Släpt ut dam ur hiss, stängt av hiss, felanmäld till XXXX. Jour.,,,,,,,,,,,,,,KÖ,,,1,50,,,1290,00 kr,,,,,,,,, Totalt arbetstid,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,1,50,,,1290,00 kr,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Material,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Make my eyes bleed... Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206900 Share on other sites More sharing options...
Muddy_Funster Posted April 27, 2011 Share Posted April 27, 2011 You do know that you can probably do what you want from within excel it's self, with filtering, some basic formulas and copy/paste special. Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206902 Share on other sites More sharing options...
weep Posted April 27, 2011 Author Share Posted April 27, 2011 Muddy_Funster: Yep, I do. But that's not what I am looking for. I have my reasons. salathe: Thanks, I'l have a ganda at it tomorrow. Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206906 Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2011 Share Posted April 27, 2011 If you save/export an Excel file as a .csv and you have fields that contain commas in the data, Excel will wrap that data inside of double-quotes. Have you actually tried what it is you are asking us about? Quote Link to comment https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/#findComment-1206913 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.