A generator parser to make streaming parsers simple.

Fund package maintenance!

Installs: 16 013 750

Dependents: 14

Suggesters: 0

Security: 0

Stars: 121

Watchers: 8

Forks: 4

Open Issues: 1

v1.1.1 2024-03-21 19:16 UTC

This package is auto-updated.

Last update: 2024-05-21 19:43:27 UTC


AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. amphp/parser allows easily building streaming generator parsers.


This package can be installed as a Composer dependency.

composer require amphp/parser


  • PHP 7.4+


PHP's generators are a great way for building incremental parsers.


This simple parser parses a line delimited protocol and prints a message for each line. Instead of printing a message, you could also invoke a data callback.

$parser = new Parser((function () {
    while (true) {
        $line = yield "\r\n";

        if (trim($line) === "") {

        print "New item: {$line}" . PHP_EOL;

for ($i = 0; $i < 100; $i++) {

Furthere examples can be found in other AMPHP packages which this library to build streaming parsers.

Yield Behavior

You can either yield a string that's used as delimiter, an integer that's used as length, or null to flush any remaining buffer in the parser (if any) or await the next call to Parser::push().


amphp/parser follows the semver semantic versioning specification like all other amphp packages.


If you discover any security related issues, please email instead of using the issue tracker.


The MIT License (MIT). Please see LICENSE for more information.