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?/m" => '
$1
\n", # paragraph "{^(
$1
', # monospace
];
return preg_replace(array_keys($markup), array_values($markup), htmlspecialchars($input));
}