| @@ -183,13 +183,14 @@ | ||
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | $buffer = min( $buffer * 2, 4096 ); |
| 186 | 186 | } |
| 187 | - | |
| 187 | + $pos = -1; | |
| 188 | 188 | if ( ! empty( $separator ) && $separator_found ) { |
| 189 | 189 | $pos = strrpos( $output, $separator ); |
| 190 | 190 | if ( $pos !== false ) { |
| 191 | 191 | $output = substr( $output, $pos + strlen( $separator ) ); |
| 192 | + $pos = -1; // we reset the position to start from the beginning of the chunk when slicing. | |
| 192 | 193 | } |
| 193 | 194 | } |
| 194 | 195 | |
| 195 | 196 | // Extract the desired number of lines |
| @@ -196,9 +197,12 @@ | ||
| 196 | 197 | $output_lines = explode( "\n", $output ); |
| 197 | 198 | if ( empty( $separator ) || ! $separator_found ) { |
| 198 | 199 | $output = implode( "\n", array_slice( $output_lines, -$lines ) ); // fetch last n lines |
| 199 | 200 | } else { |
| 200 | - $output = implode( "\n", array_slice( $output_lines, 0, $lines ) ); // fetch from separator | |
| 201 | + // Calculate the start position for slicing | |
| 202 | + $start_pos = max( $pos + 1, count( $output_lines ) - $lines ); | |
| 203 | + | |
| 204 | + $output = implode( "\n", array_slice( $output_lines, $start_pos, $lines ) ); // fetch from separator | |
| 201 | 205 | } |
| 202 | 206 | |
| 203 | 207 | fclose( $f ); |
| 204 | 208 | return trim( $output ); |