php ubb函数
封面图
•
2025-09-21
•
PHP
168
0
0
0
在 PHP 中,"UBB 函数" 通常指的是 **UBB(Ultimate Bulletin Board)** 格式解析函数。UBB 是一种类似于 BBCode 的标记语言,用于在论坛、博客等网站中实现简单的文本格式化功能。 ### 常见的 UBB 标签示例: - `[b]...[/b]`:加粗 - `[i]...[/i]`:斜体 - `[u]...[/u]`:下划线 - `[url=链接]文字[/url]`:超链接 - `[img]图片地址[/img]`:显示图片 - `[quote]...[/quote]`:引用 --- ## ✅ 如何实现一个简单的 UBB 解析函数? 下面是一个基本的 UBB 转 HTML 的 PHP 函数示例: ```php function parse_ubb($text) { // 替换 [b]...[/b] $text = preg_replace('/$$b$$](.*?)$$\/b$$/is', '<strong>$1</strong>', $text); // 替换 [i]...[/i] $text = preg_replace('/$$i$$](.*?)$$\/i$$/is', '<em>$1</em>', $text); // 替换 [u]...[/u] $text = preg_replace('/$$u$$](.*?)$$\/u$$/is', '<u>$1</u>', $text); // 替换 [url=链接]文字[/url] $text = preg_replace('/$$url=(.*?)$$](.*?)$$\/url$$/is', '<a href="$1" target="_blank">$2</a>', $text); // 替换 [img]图片地址[/img] $text = preg_replace('/$$img$$](.*?)$$\/img$$/is', '<img src="$1" alt="图片" />', $text); // 替换 [quote]...[/quote] $text = preg_replace('/$$quote$$](.*?)$$\/quote$$/is', '<blockquote>$1</blockquote>', $text); return $text; } ``` --- ## 🔧 使用示例: ```php $ubb_text = "[b]这是加粗文字[/b], [url=https://example.com]点击这里[/url]"; $html = parse_ubb($ubb_text); echo $html; ``` 输出结果: ```html <strong>这是加粗文字</strong>, <a href="https://example.com" target="_blank">点击这里</a> ``` --- ## 🚀 进阶建议: - 可以使用 `htmlspecialchars()` 避免 XSS 攻击。 - 如果你希望支持更复杂的标签(如颜色、字体大小等),可以扩展正则表达式。 - 使用 `strip_tags()` 来过滤不安全的 HTML 标签。 --- ## 📦 其他方式: 如果你不想自己写解析函数,也可以使用现成的库或框架中的 UBB 解析器,例如: - **PHPBB** 或 **SMF** 等论坛系统自带 UBB 解析。 - 第三方库如 [bbcode](https://github.com/erusev/parsedown)(虽然它是 Markdown,但可参考) --- 如果你有具体的 UBB 标签需要处理,我可以帮你定制解析函数。欢迎继续提问!
上一篇:
简单的PHP 在线人数统计
下一篇:
php mysqli类,包含常用的mysqli函数
标题录入,一次不能超过6条
返回顶部
留言
留言
评论