support more bbcode tags

audio/video/hide/hr/strikethrough
This commit is contained in:
NekoCH
2024-04-17 13:56:24 +08:00
parent 2447469ea0
commit 08f879a5b9
5 changed files with 131 additions and 5 deletions

View File

@@ -283,6 +283,20 @@ function formatYoutube($src, $width = '', $height = ''): string
));
}
function formatVideo($src, $width, $height) {
if (!$width) {
$width = 560;
}
if (!$height) {
$height = 315;
}
return addTempCode("<video controls width=\"$width\" height=\"$height\"><source src=\"$src\" /><a href=\"$src\">$src</a></video>");
}
function formatAudio($src) {
return addTempCode("<audio controls><source src=\"$src\" /><a href=\"$src\">$src</a></audio>");
}
function formatSpoiler($content, $title = '', $defaultCollapsed = true): string
{
global $lang_functions;
@@ -301,6 +315,11 @@ function formatSpoiler($content, $title = '', $defaultCollapsed = true): string
return addTempCode($HTML);
}
function formatHidden($content): string
{
return addTempCode(sprintf('<span class="hidden-text">%s</span>', $content));
}
function formatTextAlign($text, $align): string
{
return addTempCode(sprintf('<div style="text-align: %s">%s</div>', $align, $text));
@@ -346,8 +365,8 @@ function format_comment($text, $strip_html = true, $xssclean = false, $newtab =
// Linebreaks
$s = nl2br($s);
$originalBbTagArray = array('[siteurl]', '[site]','[*]', '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[pre]', '[/pre]', '[/color]', '[/font]', '[/size]', " ");
$replaceXhtmlTagArray = array(get_protocol_prefix().get_setting('basic.BASEURL'), get_setting('basic.SITENAME'), '<img class="listicon listitem" src="pic/trans.gif" alt="list" />', '<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<pre>', '</pre>', '</span>', '</font>', '</font>', ' &nbsp;');
$originalBbTagArray = array('[siteurl]', '[site]','[*]', '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[s]', '[/s]', '[pre]', '[/pre]', '[/color]', '[/font]', '[/size]', '[hr]', " ");
$replaceXhtmlTagArray = array(get_protocol_prefix().get_setting('basic.BASEURL'), get_setting('basic.SITENAME'), '&#x2022; ', '<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<s>', '</s>', '<pre>', '</pre>', '</span>', '</font>', '</font>', '<hr>', ' &nbsp;');
$s = str_replace($originalBbTagArray, $replaceXhtmlTagArray, $s);
$originalBbTagArray = array("/\[font=([^\[\(&\\;]+?)\]/is", "/\[color=([#0-9a-z]{1,15})\]/is", "/\[color=([a-z]+)\]/is", "/\[size=([1-7])\]/is");
@@ -397,6 +416,17 @@ function format_comment($text, $strip_html = true, $xssclean = false, $newtab =
$s = preg_replace_callback("/\[youtube(\,([1-9][0-9]*)\,([1-9][0-9]*))?\]((http|https):\/\/[^\s'\"<>]+)\[\/youtube\]/i", function ($matches) {
return formatYoutube($matches[4], $matches[2], $matches[3]);
}, $s);
}
if (str_contains($s, "[video")) {
$s = preg_replace_callback("/\[video(\,([1-9][0-9]*)\,([1-9][0-9]*))?\]((http|https):\/\/[^\s'\"<>]+)\[\/video\]/i", function ($matches) {
return formatVideo($matches[4], $matches[2], $matches[3]);
}, $s);
}
if (str_contains($s, "[audio")) {
$s = preg_replace_callback("/\[audio\]((http|https):\/\/[^\s'\"<>]+)\[\/audio\]/i", function ($matches) {
return formatAudio($matches[1]);
}, $s);
}
// [url=http://www.example.com]Text[/url]
@@ -433,6 +463,11 @@ function format_comment($text, $strip_html = true, $xssclean = false, $newtab =
return formatTextAlign($matches[1], 'right');
}, $s);
// [hide]Hidden text[/hide]
$s = preg_replace_callback("/\[hide\](.*)\[\/hide\]/isU", function ($matches) {
return formatHidden($matches[1]);
}, $s);
$s = format_urls($s, $newtab);
// Quotes
@@ -5557,7 +5592,7 @@ function strip_all_tags($text)
{
//替换掉无参数标签
$bbTags = [
'[*]', '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[pre]', '[/pre]', '[quote]', '[/quote]',
'[*]', '[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', '[s]', '[/s]', '[pre]', '[/pre]', '[quote]', '[/quote]',
'[/color]', '[/font]', '[/size]', '[/url]', '[/youtube]', '[/spoiler]',
];
$text = str_replace($bbTags, '', $text);