3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyFilter extends php_user_filter { private $data = ''; public function filter($in, $out, &$consumed, $closing) { $return = PSFS_FEED_ME; // While input data is available, continue to read it. while ($bucket_in = stream_bucket_make_writeable($in)) { $this->data .= $bucket_in->data; $consumed += $bucket_in->datalen; // Process whole lines. while (preg_match('/(.*?)[\r\n]+(.*)/s', $this->data, $match) === 1) { list(, $data, $this->data) = $match; // Send this record output. $data = strrev($data) . PHP_EOL; $bucket_out = stream_bucket_new($this->stream, $data); $return = PSFS_PASS_ON; stream_bucket_append($out, $bucket_out); } } // Process the final line. if ($closing && $this->data !== '') { $data = strrev($this->data) . PHP_EOL; $bucket_out = stream_bucket_new($this->stream, $data); $return = PSFS_PASS_ON; stream_bucket_append($out, $bucket_out); } return $return; } } stream_filter_register('my-filter', MyFilter::class); $input = "Line one\nLine two\nLine three"; $stream = fopen('data://text/plain,' . $input, 'r'); stream_filter_append($stream, 'my-filter'); $output = ''; while (!feof($stream)) { $output .= fread($stream, 16); } fclose($stream); echo $output;
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Deprecated: Return type of MyFilter::filter($in, $out, &$consumed, $closing) should either be compatible with php_user_filter::filter($in, $out, &$consumed, bool $closing): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/LDoHh on line 5 eno eniL owt eniL eerht eniL
Output for 5.6.5 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.4.14 - 7.4.33, 8.0.1 - 8.0.30
eno eniL owt eniL eerht eniL
Output for 5.6.0 - 5.6.4, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.13, 8.0.0
eno eniL owt eniL

preferences:
256.41 ms | 402 KiB | 292 Q