3v4l.org

run code in 300+ PHP versions simultaneously
<?php class StringStreamWrapper { const PROTOCOL = "stringstream"; private $stringstream; private $position; private $stringlength; public function stream_open($path, $mode, $options, &$opened_path) { $contextOptions = stream_context_get_options($this->context); if (!isset($contextOptions[self::PROTOCOL]['string'])) return false; $this->position = 0; // this is our stream! $this->stringstream = $contextOptions[self::PROTOCOL]['string']; $this->stringlength = strlen($this->stringstream); return true; } public function stream_read($len) { $data = substr($this->stringstream, $this->position, $len); $this->position += strlen($data); return $data; } public function stream_write($data){ $l = strlen($data); $this->stringstream = substr($this->stringstream, 0, $this->position) . $data . substr($this->stringstream, $this->position += $l); $this->stringlength = strlen($this->stringstream); return $l; } public function stream_seek($offset, $whence = SEEK_SET) { if ($whence == SEEK_CUR) { $this->position += $offset; } else if ($whence == SEEK_END) { $this->position = $this->stringlength + $offset; } else { $this->position = $offset; } return true; } public function stream_tell() { return $this->position; } public function stream_eof() { return ($this->position >= $this->stringlength); } public function stream_stat() { return array( 7 => $this->stringlength, 'size' => $this->stringlength, ); } static public function Open($string) { $context = stream_context_create(array(self::PROTOCOL => array('string' => &$string))); return fopen(self::PROTOCOL . "://",'r', false, $context); } } stream_wrapper_register(StringStreamWrapper::PROTOCOL, "StringStreamWrapper"); ####################################### $testString = 'test'; echo "base64_encode = " . base64_encode($testString).PHP_EOL; ####################################### # put data in the StringStreamWrapper $stringstream = StringStreamWrapper::Open($testString); # get the output stream (as z-push does) $output = fopen("php://output", "w+"); # attach write filter to output $filter = stream_filter_append($output, 'convert.base64-encode'); echo "outputstream = "; # write to output $bytes = stream_copy_to_stream($stringstream, $output); write($output, chr(0)); echo "\n wrote $bytes";
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
base64_encode = dGVzdA== Deprecated: Creation of dynamic property StringStreamWrapper::$context is deprecated in /in/3ZmVB on line 68 outputstream = dGVz Fatal error: Uncaught Error: Call to undefined function write() in /in/3ZmVB:94 Stack trace: #0 {main} thrown in /in/3ZmVB on line 94 dA==
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
base64_encode = dGVzdA== outputstream = dGVz Fatal error: Uncaught Error: Call to undefined function write() in /in/3ZmVB:94 Stack trace: #0 {main} thrown in /in/3ZmVB on line 94 dA==
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
base64_encode = dGVzdA== outputstream = dGVz Fatal error: Call to undefined function write() in /in/3ZmVB on line 94 dA==
Process exited with code 255.

preferences:
155.55 ms | 402 KiB | 183 Q