Skip to content

Commit 668e96f

Browse files
committed
Add Stream method to get output between line numbers
1 parent 63012fe commit 668e96f

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

Stream.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,40 @@ public function tell(): int
175175
}
176176

177177
/**
178-
* Gets line from file pointer
178+
* Gets line from a file pointer
179179
* @return string|false
180180
*/
181-
public function getLine(): string|bool
181+
public function getLine(): string|false
182182
{
183-
$line = fgets($this->resource);
184-
return trim($line);
183+
return fgets($this->resource);
185184
}
186185

186+
/**
187+
* Will get output between a from and to line number
188+
*
189+
* @param int $from
190+
* @param int $to
191+
* @return string
192+
*/
193+
public function getLines(int $from, int $to): string
194+
{
195+
$this->rewind();
196+
$lineNo = 0;
197+
$out = '';
198+
while (($line = $this->getLine()) !== false) {
199+
++$lineNo;
200+
if ($lineNo < $from) {
201+
continue;
202+
}
203+
if ($lineNo > $to) {
204+
break;
205+
}
206+
$out .= $line;
207+
}
208+
return $out;
209+
}
210+
211+
187212
/**
188213
* Returns true if the stream is at the end of the stream.
189214
* @return bool

0 commit comments

Comments
 (0)