Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中处理简单标记到html转换器的更好方法_Php_Regex_Preg Replace_Preg Match All - Fatal编程技术网

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

在php中处理简单标记到html转换器的更好方法,php,regex,preg-replace,preg-match-all,Php,Regex,Preg Replace,Preg Match All,是的,我知道已经有一些类/包/系统为我做了这些,但我有一些要求和设计选择,阻止我使用它们。既然我已经决定实现我自己的简单标记,有没有比现在更好的方法来处理标题 // Basic markup, based on markdown public static function MarkupToHtml($text) { $text = Util::cleanup($text); $text = preg_replace('/^[ ]+$/m', '', $text); /

是的,我知道已经有一些类/包/系统为我做了这些,但我有一些要求和设计选择,阻止我使用它们。既然我已经决定实现我自己的简单标记,有没有比现在更好的方法来处理标题

// 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;
}
//基本标记,基于标记
公共静态函数MarkupToHtml($text){
$text=Util::cleanup($text);
$text=preg_replace('/^[]+$/m',''.$text);
//在标题后添加换行符
//所以段落可以正常工作。应该算出正则表达式,这样它就不会
//如果不需要,请添加额外的\n
$text=preg_replace('{(^ |\n)([=]+)(.*?(\n)}',“$0\n”,$text);
//段落
//忽略标题行
$text=preg_replace('{(\n\n | ^)([^=])(.\n)*?(?=\n\n |$)}','$0

',$text); //标题 //这是可行的,但是有没有更干净的方法呢 preg_match_all(“/(^|\n)([=]+)(.*?(\n)/”,$text,$matches,preg_SET_顺序); foreach($匹配为$val){ $num=intval(strlen($val[2]))+2; 如果($num>5){ $num=5; } $text=str_replace($val[0],“”.$val[3],$text); } //大胆的 $text=preg_replace(“{([*])(.*?([*])}”,“$2”,$text); //斜体 $text=preg_replace('{([[u])(.*?([[u])}','$2',$text); //单声道 $text=preg_replace('{([`])(.*?([`])}',“$2”,$text); 返回$text; }
使用预匹配回调:

$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
    );
$line=preg\u replace\u回调(
“/(^ |\n)([=]+)(.*)(\n)/”,
创建函数(
“$matches”,
“$num=min(intval(strlen($matches[2]))+2,5);”。
“return”。$matches[3]。;”
),
美元线
);

更好的方法是使用现有的图书馆,正如问题的第一句所解释的,这不是一个选项。谢谢你的无意义的评论。如果它有效,不要修复它。