<?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));
You have javascript disabled. You will not be able to edit any code.