php将markdown转成BBCode代码

php 文章 2021-03-07 14:16 0 38 全屏看文

public static function markdown_2_bbcode($text)

{

$text = htmlspecialchars_decode($text);


$text = preg_replace('/\*\*((?:(?!\*\*).)+)\*\*/', '\1', $text);

$text = preg_replace('/\*((?:(?!\*).)+)\*/', '\1', $text);

$text = preg_replace('/##((?:(?!##).)+)(?:##)?/', '\[size=16]\1\[\/size]', $text);

$text = preg_replace('/!!\[(?:(?!\]).)*\]\(((?:(?!\)).)+)\)/', '

', $text);

$text = preg_replace('/!\[(?:(?!\]).)*\]\(((?:(?!\)).)+)\)/', '', $text);

$text = preg_replace('/\[((?:(?!\]).)+)\]\(((?:(?!\)).)+)\)/', '', $text);

$text = preg_replace('/{{{(.+?)}}}/s', '

\1
', $text);

$text = preg_replace('/^>((?:(?!\n\n).)+)/ms', '

\1

', $text);


preg_match_all('/(^\d+\. .+\n?)+/m', $text, $num_list);

if ($num_list[0])

{

foreach ($num_list[0] AS $value)

{

$new_value = trim(preg_replace('/^\d+\. (.+)/m', '[*]\1[/*]', $value));

$text = str_replace($value, "

    \n$new_value\n
", $text);

}

}


preg_match_all('/(^- .+\n?)+/m', $text, $nor_list);

if ($nor_list[0])

{

foreach ($nor_list[0] AS $value)

{

$new_value = trim(preg_replace('/^- (.+)/m', '[*]\1[/*]', $value));

$text = str_replace($value, "

    \n$new_value\n
", $text);

}

}


return htmlspecialchars($text);

}


-EOF-