0

I am currently using php to open a csv file, check down for a cell containing particular content and using if-else to gain spicific information. However, I need to check if a cell contains PART of some information.

e.g.

current code:

$error_handle = fopen("$reportUrl", "r");
while (!feof($error_handle) )
{
    $line_of_text = fgetcsv($error_handle, 1024);
    if($line_of_text[0] !== "")
        {
            $countErrors++;
        }
}
fclose($error_handle);

code I want:

$error_handle = fopen("$reportUrl", "r");
while (!feof($error_handle) )
{
    $line_of_text = fgetcsv($error_handle, 1024);
    if($line_of_text[4] == "$date *")
        {
            $countErrors++;
        }
}
fclose($error_handle);

the '*' is representing a time that will be different for each line of text so can't be used but is part of the same cell. How do I select the cells with the same date but different times?

FYI, the date format would usually be '15/01/2013 12:06pm'

hakre
  • 193,403
  • 52
  • 435
  • 836
Alan Doolan
  • 175
  • 1
  • 8
  • 17
  • You should select @Dutow 's answer as correct since you used that solution. – MECU Jan 16 '13 at 14:09
  • I didn't, I was adding it up before he said it although mine might be wrong as he is checking if it equals zero while I'm checking if it isn't false – Alan Doolan Jan 16 '13 at 14:10
  • @AlanDoolan: *"I think I've got it:"* - Sorry dude, but please *accept the answer below* - even if you did not copy it from there, it's good practice you accept the right answer below. And don't edit your question, I've moved your "answer" into the existing one. – hakre Jan 16 '13 at 14:11
  • sorry, couldn't find the 'answer my own question' button at the time. – Alan Doolan Jan 16 '13 at 14:16
  • @AlanDoolan: No problem, just saying and leaving a comment therefore. I also added another answer which might give you some future pointers to generalize such problems. – hakre Jan 16 '13 at 14:29

4 Answers4

1

Use the strpos function:

if(strpos($line_of_text[4], "$date ") ===0) { found }

Think I've got it:

I'm using the following:

        while (!feof($error_handle) )
        {
            $line_of_text = fgetcsv($error_handle, 1024);
            if(strpos($line_of_text[4], $date) !== false)
                {
                    $countErrors++;
                }
        }
    fclose($error_handle);

which seems to be working

hakre
  • 193,403
  • 52
  • 435
  • 836
Dutow
  • 5,638
  • 1
  • 30
  • 40
  • strpos() would be better. Also take a look at substr() to extract date from a datetime string. – MFix Jan 16 '13 at 14:01
  • Hey, that is what I am using however you are using '=== 0' while I'm using '!== false'. why would I be looking for it to be 0? – Alan Doolan Jan 16 '13 at 14:09
  • your original example ("$date *") contained the date as the first part of the string. !== false returns true always when the string contains $date. Check both results for "something $date something" – Dutow Jan 16 '13 at 14:12
  • ok, so both work but mine will work even if something is before the date while t === 0 will work if it is only the date followed by something? – Alan Doolan Jan 16 '13 at 14:15
  • Does 0 not count as false in this situation? if so, would that not mean that each time it finds the date it will be false? – Alan Doolan Jan 16 '13 at 14:18
  • That's why there is three = symbol in the if, not just two. [See this question about this](http://stackoverflow.com/questions/2063480/the-3-different-equals) – Dutow Jan 16 '13 at 14:19
0

you can store the timestamps of the date & time instead of '15/01/2013 12:06pm' or something into the file so you can retrieve the timestamp and format the date as you wish.

Mithun Sen
  • 523
  • 5
  • 19
0
    while (!feof($error_handle) )
        {
            $line_of_text = fgetcsv($error_handle, 1024);
            if(strpos($line_of_text[4], $date) !== false)
                {
                    $countErrors++;
                }
        }
    fclose($error_handle);
Alan Doolan
  • 175
  • 1
  • 8
  • 17
0

You can also make use of SplFileObject setting it to CSV mode, then filtering the rows based on your condition (cell index 4, your $date string comparison) and counting (see iterator_count):

$rows     = new SplFileObject($reportUrl);
$rows->setFlags($rows::READ_CSV);
$filtered = new CallbackFilterIterator($rows, function ($current) use ($date) {
    return strpos($current[4], "$date ") === 0;
});
$countErrors = iterator_count($filtered);
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I'm going to have to do some reading on this but the way I am currently doing it is reading each line of the csv and checking if a particular cell contains something. Is it highly inefficient though but would your suggestion be better in this regard? – Alan Doolan Jan 16 '13 at 14:33
  • Going through each row of your CSV file is implemented in `SplFileObject` when you set the flags to `READ_CSV`, you can just use it in a `foreach` for each line as an array containing the cells. I additionally create a filter on all those lines in my example and then count the outcome. That is using PHP's nice iterators and the nice `SplFileObject` takes care of CSV parsing while you only need to `foreach`-it. Easy peasy. – hakre Jan 16 '13 at 14:42
  • thanks very much. The other fixes this particular problem but I will look into the other. – Alan Doolan Jan 16 '13 at 15:20