3v4l.org

run code in 300+ PHP versions simultaneously
<?php $text = <<<TEXT == My Subheading == === My sub Subheading === ==== another heading ==== TEXT; class ContentParser { private $patterns = []; private $replacements = []; public function __construct() { $this->patterns = [ // Headings "/^==== (.+?) ====\h*$/m", "/^=== (.+?) ===\h*$/m", "/^== (.+?) ==\h*$/m", ]; $this->replacements = [ // Headings "<h3>$1</h3>", "<h2>$1</h2>", "<h1>$1</h1>", ]; } public function parse($input) { if (!empty($input)) { $output = preg_replace($this->patterns, $this->replacements, $input); } else { $output = false; // I don't recommend returning a boolean when otherwise returning strings } return $output; } } $object = new ContentParser(); var_export($object->parse($text));
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
'<h1>My Subheading</h1> <h2>My sub Subheading</h2> <h3>another heading</h3>'

preferences:
106.31 ms | 1373 KiB | 4 Q