找到文件中的下一个空行

I'm working on a script that edits PHP files contents. So far, i'm able to check if the line is empty on the file and then write what I need into it. However I need to find a way to loop through the array until it finds the next empty line if the first query was not empty.

For example, I want to edit this PHP file - example.php - which contains the following:

<?php

I am not an empty line.

I am not an empty line.

I am not an empty line.

?>

My script:

// File variables
$file = 'path/example.php';
$content = '';

// Check if the file exists and is readable 
if (file_exists($file) && is_readable($file)) {

    $content = file_get_contents($file);

}

// put lines into an array
$lines = explode("
", $content);

//Get the fourth line
$Getline = $lines[3];

// check if the line is emptpy
if (empty($Getline) && $Getline !== '0') {

   // Write something in the file
}
else {

  // Find the next empty line

}

So all I need is to loop through the array until it finds the next empty line. Although I'm not sure how do that.

Use PHP file() function instead of file_get_contents() function. It will read the file in array format itself.

Then you can parse this array using foreach() and can check blank value in it.

May this will help you.

<?php
foreach($lines as $line)
{
    // check if the line is empty
    if (empty($line) || $line == '')
    {
        //Line = empty and do stuff here.
    }
}
?>