(.*?)<\/h\1>/i', function ($obj) { global $catalog; global $catalog_count; $catalog_count++; $catalog[] = array('text' => trim(strip_tags($obj[3])), 'depth' => $obj[1], 'count' => $catalog_count); return '' . $obj[3] . ''; }, $obj); return $obj; } // 灯箱 function replaceImgSrc($content) { $pattern = '//i'; $replacement = ''; return preg_replace($pattern, $replacement, $content); } // 提示短代码 function ContentHint($content) { $patterns = array( '/\[(success)\]\s*(.*?)\s*\[\s*\/\1\s*\]/s', '/\[(info)\]\s*(.*?)\s*\[\s*\/\1\s*\]/s', '/\[(warning)\]\s*(.*?)\s*\[\s*\/\1\s*\]/s', '/\[(danger)\]\s*(.*?)\s*\[\s*\/\1\s*\]/s', ); $replacements = array( 'success' => '
%s
', 'info' => '
%s
', 'warning' => '
%s
', 'danger' => '
%s
', ); $callback = function ($matches) use ($replacements) { $type = $matches[1]; // 获取匹配的类型 (success, info, warning, danger) // 去除开头的
标签 $text = preg_replace('/^()+/i', '', $matches[2]); $text = Markdown::convert($text); // 转换 Markdown return sprintf($replacements[$type], $text); // 替换内容 }; foreach ($patterns as $pattern) { $content = preg_replace_callback($pattern, $callback, $content); } return $content; } // 视频短代码 function ContentVideo($content) { $pattern = '/\[player\s+url="([^"]*)"(?:\s+pic="([^"]*)")?\s+\/\]/'; $content = preg_replace_callback($pattern, function ($matches) { $videoSrc = @strip_tags($matches[1]); // 获取 url 的值 $posterSrc = @strip_tags($matches[2]); // 获取 pic 的值 if (empty($posterSrc)) { $posterSrc = getAssets('assets/images/thumb/' . rand(1, 20) . '.webp', false); } return ''; }, $content); return $content; } // 链接短代码 function extractToLinks($content) { $pattern = '/\[tolink\s+title="([^"]*)"\s+url="([^"]*)"(?:\s+favicon="([^"]*)")?\s+\/\]/'; $content = preg_replace_callback($pattern, function ($matches) { $tohtml = ''; return $tohtml; }, $content); return $content; } // 过滤a标签链接 function ContentLink($content) { if (empty(Helper::options()->isGoLink)) { return $content; } // 正则表达式匹配所有的标签及其属性 $pattern = '/]*?)>/i'; // 替换回调函数 $content = preg_replace_callback($pattern, function ($matches) { $attributes = $matches[1]; // 检查是否包含需要排除的属性 if (preg_match('/\b(data-fancybox|data-cloud|data-test2)\b/', $attributes)) { // 包含需要排除的属性,不处理 return ''; } // 获取href属性 if (preg_match('/\bhref=["\']([^"\']*)["\']/i', $attributes, $hrefMatches)) { $originalHref = $hrefMatches[1]; // 构造新的href $newHref = getGoLink($originalHref); // 替换href属性 $newAttributes = preg_replace('/\bhref=["\'][^"\']*["\']/i', 'href="' . $newHref . '"', $attributes); // 检查是否已存在rel或target属性 if (strpos($attributes, 'rel=') === false) { $newAttributes .= ' rel="nofollow"'; } if (strpos($attributes, 'target=') === false) { $newAttributes .= ' target="_blank"'; } return ''; } // 没有href属性,保持不变 return ''; }, $content); return $content; } // 过滤网盘下载 function ContentCloud($content) { $pattern = '/\[cloud\s+type="([^"]*)"\s+title="([^"]*)"\s+url="([^"]*)"\s+password="([^"]*)"\s+\/\]/'; $cloudList = array( 'default' => '默认网盘', 'baidu' => '百度网盘', 'quark' => '夸克网盘', 'aliyun' => '阿里云网盘', 'lanzou' => '蓝奏云网盘', '360' => '360网盘', 'weiyun' => '腾讯微云', 'ctfile' => '城通网盘', 'github' => 'GitHub仓库', ); $content = preg_replace_callback($pattern, function ($matches) use ($cloudList) { // 提取属性值 $type = @strip_tags($matches[1]); // 获取 type 的值 $title = @strip_tags($matches[2]); // 获取 title 的值 $url = @strip_tags($matches[3]); // 获取 url 的值 $password = @strip_tags($matches[4]); // 获取 password 的值 // 根据 type 获取网盘名称 $name = isset($cloudList[$type]) ? $cloudList[$type] : '未知网盘'; $password = $password ? $password : '无'; $icon = '
'; $cloudHtml = '
'; $cloudHtml .= '
' . $icon . '
'; $cloudHtml .= '
'; $cloudHtml .= ''; $cloudHtml .= '
提取码: ' . $password . '
'; $cloudHtml .= '
来源:' . $name . '
'; $cloudHtml .= '
'; $cloudHtml .= '
'; return $cloudHtml; }, $content); return $content; } // 过滤pre代码标签 function ContentCode($content) { // 正则表达式匹配 $pattern = '#]*)>(.*?)#si'; // 替换的 HTML 结构 $replacement = '
$2
'; $content = preg_replace($pattern, $replacement, $content); return $content; } // 过滤fold折叠框 function ContentFold($content) { $pattern = '#\[fold\s+title="([^"]*)"\s+type="(open|close)"\s*\](.*?)\[/fold\]#si'; $content = preg_replace_callback($pattern, function ($matches) { $title = $matches[1]; $type = $matches[2]; $contentText = Markdown::convert($matches[3]); $openAttr = ($type === 'open') ? 'open' : ''; return '
' . $title . '
' . $contentText . '
'; }, $content); return $content; } // 过滤多余的html标签 function ContentHtml($content) { // 使用一个正则表达式同时匹配并删除空段落和仅包含两个换行符的段落 $content = preg_replace('#

|

#si', '', $content); return $content; } // 运行所以函数 function parseContens($content) { // 添加文章标题锚点 $content = createAnchor($content); // 添加图片懒加载 $content = replaceImgSrc($content); // 提示短代码 $content = ContentHint($content); // 视频短代码 $content = ContentVideo($content); // 链接短代码 $content = extractToLinks($content); // 网盘下载短代码 $content = ContentCloud($content); // 过滤a标签链接添加golink $content = ContentLink($content); // 表情包 $content = formatEmoji($content); // 过滤pre代码标签 $content = ContentCode($content); // 过滤fold折叠框 $content = ContentFold($content); // 过滤多余的html标签 $content = ContentHtml($content); return $content; } ?>