如何在PHP中第n次出现字符串到第n次出现?

I am trying to parse a huge string into smaller ones in PHP. I have a string that ends with for each part. I want to get the sentence after the 2nd till the 5th .

I managed to that in a way but since the string that I'm dealing with is huge, it takes loads of time. This is the method I used:

$arrayBuffer = explode("
", $buffer);
for($i = 0; $i < $NumOfRequests; $i++) {
    $tmpBuffer = "";
    for($j = $i * $NumOfAllowedRows; $j < ($i + 1) * $NumOfAllowedRows; $j++) {
        // stop the loop if it reached the last cell of arrayBuffer, to avoid Index Out Of Bound Exception
        if($j === $arrayBuffer[count($arrayBuffer)]){
            break;
        }
        $tmpBuffer = $tmpBuffer . $arrayBuffer[$j] . "
";                  
    }
         // do something with the tmpBuffer                 
}

The purpose is to take a substring from the first $buffer itself without creating another array.

I found a good solution using explode and implode and it is fast enough, Here is an example to the code :

$buffer = "aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
"; // enter file path here
for($i = 0; $i < 5 ; $i++){

$arr = explode("
", $buffer);
//strlen(implode("
", array_slice($arr, 0, 6)));
$position = (strlen(implode("
", array_slice($arr, 0, 3))));

$tmpBuffer = substr($buffer,0,$position);
$buffer = substr($buffer, $position);

error_log("tmpbuffer : ".$tmpBuffer).PHP_EOL;
error_log("buffer : " . $buffer).PHP_EOL;

explode will divide the string into peaces, then I get the character's nth occurrence in the string, strlen will then get the length of the sub-string till the nth occurrence.