Save a line only if not matched

I'm trying to save data from file in case the line is not matched with a character. In my case, I have a list of numbers, I want to save these numbers if only the first character is not equal to "0" -zero-

Here is my code:

<?php
$cleanfile = "cleanfile.txt";
$handle = fopen("file.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if ($line[0] != 0) {
            $save = file_put_contents($cleanfile, $line);
        }
    }

    fclose($handle);
} else {
    // error opening the file.
} 

My file.txt has the following entries:

1
2
3457
94
31
54
039
3114
94
01
33333
1
2
3457
94
31
54
039
3114
94
01
33333
1
2
3457
94
31
54
039
3114
94
01
33333


Read more here: https://stackoverflow.com/questions/64944842/save-a-line-only-if-not-matched

Content Attribution

This content was originally published by Zack at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: