3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Parser { private $chunkSize; private $buffer = ''; private $callback; public function __construct($chunkSize) { $this->chunkSize = $chunkSize; } public function onChunk($callback) { $this->callback = $callback; } public function parse($data) { $this->buffer .= $data; while (strlen($data) >= $this->chunkSize) { var_dump($this->buffer); $chunk = (string)substr($this->buffer, 0, $this->chunkSize); $this->buffer = (string)substr($this->buffer, $this->chunkSize); call_user_func($this->callback, $chunk); } } } $p = new Parser(43); $p->onChunk(function($data) { echo "Got chunk of data: {$data}\n"; }); $p->parse('The quick brown fox jumps over the lazy dogThe quick brown fox'); $p->parse(' jum'); $p->parse('ps'); $p->parse(' over the lazy dogThe quick brown fox jumps '); $p->parse('over the lazy dog');