admin and preg_match('/\A<[a-z][^>]*>/', $input)) { return $input; # allow html input as is if privileged } $markup = [ '{<((?:\w+:|/).+?)>}' => '<$1>', # unescape link entities '{<(?:https?://)?([^>\s|]+)>}' => '<$1 $1>', # unnamed link '{<([^>\s|]+)[\s|]([^>]+)>}' => '$2', # hyperlink "/\r\n?/" => "\n", # unix newlines "/ +\n/" => "
", # trailing spaces for hard line break "/\n/" => "

\n

", # newlines start paragraphs '/\b_(\w+)_\b/' => '$1', # italic '/\b\*(\w+)\*\b/' => '$1', # bold ]; $html = preg_replace(array_keys($markup), array_values($markup), htmlspecialchars($input)); return "

$html

"; }