PHP - Incrementing multiple values in a text file with explode

123 views Asked by At

I'm trying to increment a counter inside a text file, that counts HTTP responses from some APIs. This part is handled correctly at the moment. The issue I'm having is that when my text file has more than two values that need to be incremented, only one of the fields is updated and I'm not sure what the structure of the explode should be to update the other value.

The structure of the text file is:

api:one
success:1
fail:1

and no matter what response is received, only the fail is updated.

The code section I have at the moment is:

if ($status == "6" OR $status == "2") {
    $filename = '/home/path/to/file/'.basename(__DIR__).'-responses.txt';
    $lines = file($filename);
    if(!is_file($filename)){
        $default = 'api:one' . "\n" . 'success:1' . "\n" . 'fail:1';
        file_put_contents($filename, $default);
    
    } else {
        
        foreach ($lines as $k=>$v) { 
            $exploded = explode(":", $v);

            if ($exploded[0] == "fail") {
                $exploded[1]++;
                $lines[$k] = implode(":", $exploded);
            }        
        }
        file_put_contents($filename, $lines);
    }
} else {
    $filename = '/home/path/to/file/'.basename(__DIR__).'-responses.txt';
    $lines = file($filename);
    if(!is_file($filename)){
        $default = 'api:one' . "\n" . 'success:1' . "\n" . 'fail:1';
        file_put_contents($filename, $default);
    
    } else {
        
        foreach ($lines as $k=>$v) { 
            $exploded = explode(":", $v);

            if ($exploded[0] == "success") {
                $exploded[1]++;
                $lines[$k] = implode(":", $exploded);
            }        
        }
        file_put_contents($filename, $lines);
    }
}
1

There are 1 answers

3
Barmar On BEST ANSWER

The problem is that you're not removing the newlines from the lines before you try to increment the numbers. The success line has a newline at the end, and when you try to increment "1\n" it doesn't do anything, since that's not a number.

Use the FILE_IGNORE_NEW_LINES option to remove the newlines when creating the array, then add them back when writing back to the file.

if ($status == "6" OR $status == "2") {
    $filename = '/home/path/to/file/'.basename(__DIR__).'-responses.txt';
    if(!is_file($filename)){
        $default = 'api:one' . "\n" . 'success:1' . "\n" . 'fail:1';
        file_put_contents($filename, $default);
    } else {
        $lines = file($filename, FILE_IGNORE_NEW_LINES);
        foreach ($lines as $k=>$v) { 
            $exploded = explode(":", $v);

            if ($exploded[0] == "fail") {
                $exploded[1]++;
                $lines[$k] = implode(":", $exploded);
            }        
        }
        file_put_contents($filename, implode("\n", $lines));
    }
} else {
    $filename = '/home/path/to/file/'.basename(__DIR__).'-responses.txt';
    if(!is_file($filename)){
        $default = 'api:one' . "\n" . 'success:1' . "\n" . 'fail:1';
        file_put_contents($filename, $default);
    } else {
        $lines = file($filename, FILE_IGNORE_NEW_LINES);
        foreach ($lines as $k=>$v) { 
            $exploded = explode(":", $v);

            if ($exploded[0] == "success") {
                $exploded[1]++;
                $lines[$k] = implode(":", $exploded);
            }        
        }
        file_put_contents($filename, implode("\n", $lines));
    }
}

Also, you should do the check for whether the file exists before you try to read the file.