是的,我知道已经有类/包/系统可以为我做这件事,但是我有一些要求和设计选择使我无法使用它们。因此,鉴于我已经决定实现自己的简单标记,是否有比我目前正在做的更好的方法来处理标题?
// Basic markup, based on markdown
public static function MarkupToHtml($text) {
$text = Util::cleanup($text);
$text = preg_replace('/^[ ]+$/m', '', $text);
// Add a newline after headers
// so paragraphs work properly. Should figure out regex so it doesn't
// add an extra \n if its not needed
$text = preg_replace('{(^|\n)([=]+)(.*?)(\n)}', "$0\n", $text);
// Paragraphs
// Ignore header lines
$text = preg_replace('{(\n\n|^)([^=])(.|\n)*?(?=\n\n|$)}', '<p>$0</p>', $text);
// Headers
// This works, but is there a cleaner way to go about it
preg_match_all ("/(^|\n)([=]+)(.*?)(\n)/", $text, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
$num = intval(strlen($val[2])) + 2;
if ($num > 5) {
$num = 5;
}
$text = str_replace($val[0], "<h" . $num . ">" . $val[3] . "</h" . $num .">", $text);
}
// Bold
$text = preg_replace('{([*])(.*?)([*])}', '<strong>$2</strong>', $text);
// Italic
$text = preg_replace('{([_])(.*?)([_])}', '<em>$2</em>', $text);
// mono
$text = preg_replace('{([`])(.*?)([`])}', "<span style='font-family:monospace;'>$2</span>", $text);
return $text;
}