I have a file that turns out to have an empty Righa at the beginning and end of two empty lines wing generating me an error

```
<?php
$file = "https://example.com/text/file.txt";

$fr = fopen($file, 'r');

$contenuto = "";
while (!feof($fr)) {

    $riga = fgets($fr);

    $inizio = strpos($riga, "Capitolo");

    $nuova_riga = substr($riga, $inizio);

    $contenuto .= $nuova_riga . "";
}
fclose($fr);
$fr = fopen("file.txt", 'w');
fwrite($fr, $contenuto);
fclose($fr);

```

Notice: Undefined offset:
Notice: Undefined index: 

if you manually delete blank lines obviously I do not have the mistake, so I should open the file for writing suppress blank lines and then continue with the other instructions on the page. How can I do? Thank you