Download the PHP package jstewmc/stream without Composer
On this page you can find all versions of the php package jstewmc/stream. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package stream
Stream
A multi-byte-safe stream for reading very large files (or strings) character-by-character.
Reading very large files or strings into PHP's memory and exploding them for character-by-character processing - like parsing or lexing - can quickly overrun your process memory.
This library streams the characters of very large text files in an easy, multi-byte, memory-safe manner:
The (trivial) example above would produce the following output:
In the background, this library uses jstewmc/chunker to chunk very large text files or strings in a multi-byte, low-memory manner and moves between chunks as necessary.
Installation
This library requires PHP 7.4+.
It is multi-platform, and we strive to make it run equally well on Windows, Linux, and OSX.
It should be installed via Composer. To do so, add the following line to the require
section of your composer.json
file, and run composer update
:
Usage
Instantiating a stream
A stream can be instantiated as Text
or File
:
By default, a stream uses the environment's character encoding and a chunk size of around 8kb. If you need more control, you can instantiate a stream using a Chunker
instance, instead of a string:
Navigating a stream
Once a stream has been instantiated, you can get the stream's current, next, and previous characters using the getCurrentCharacter()
, getNextCharacter()
, and getPreviousCharacter()
methods, respectively (these methods are aliased as current()
, next()
, and previous()
, respectively, and they will return false
if the character does not exist):
These methods will typically be combined in a while
loop like so:
Keep in mind, these methods are idempotent and repeatable. For example, you can call next()
multiple times at the end of the stream without proceeding past the end of the stream, and you can call previous()
from the end of the stream to navigate in the opposite direction.
Peaking ahead
You can use the peek()
method to look ahead to the next n characters without updating the internal index:
Testing the content
You can use the isOn()
method to test whether or not the stream is on a string or includes one of an array of strings:
You can use the isOnRegex()
method to test whether or not a number of characters match the given regular expression (rather than attempt to detect the number of characters in the regular expression, which would be very difficult, the number of characters to search is the second argument):
Resetting the stream
If you need to, you can reset the stream's internal pointer:
License
This library is released under the MIT license.
Contributing
Contributions are welcome!
Here are the steps to get started: