在php中处理简单的标记到html转换器的更好方法


A better way to handle a simple markup to html converter in php

是的,我知道已经有类/包/系统为我做了这些,但是我有一些要求和设计选择使我无法使用它们。因此,考虑到我已经决定实现自己的简单标记,是否有比我目前做的更好的方法来处理标头?

// 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;
}

use preg_match_callback:

$line = preg_replace_callback(
        '/(^|'n)([=]+)(.*?)('n)/',
        create_function(
            '$matches',
            '$num = min(intval(strlen($matches[2])) + 2,5);' .
            'return "<h" . $num . ">" . $matches[3] . "</h" . $num .">";'
        ),
        $line
    );