Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/234849-csv-ignoring-empty-fields/
Share on other sites

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

 

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.

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

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...

 

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.