想象一下,你正在为你的新社区平台构建一个帖子发布功能。用户希望能够使用
[b]粗体[/b]
[url=http://example.com]链接[/url]
[quote author="张三"]引用内容[/quote]
然而,当你开始动手实现解析逻辑时,噩梦便开始了。你需要编写复杂的正则表达式来匹配各种标签,处理它们的属性,解析标签内的内容,还要考虑标签的正确闭合、嵌套关系,以及如何优雅地处理错误或恶意输入。一个不小心,就可能导致页面布局错乱,甚至被注入恶意脚本。每次新增一个短代码,都意味着可能要修改一大段正则代码,测试成本高昂,维护更是让人头秃。我曾为此投入了大量时间,却依然无法保证解析的完美无缺和绝对安全。
thunderer/shortcode
幸运的是,PHP生态系统中有Composer这个强大的包管理工具,它为我们带来了无数优秀的第三方库,其中就包括
thunderer/shortcode
thunderer/shortcode
[b]
[url]
使用
thunderer/shortcode
<pre class="brush:php;toolbar:false;">composer require thunderer/shortcode=^0.7
安装完成后,你就可以开始享受它带来的便利了。
让我们以一个常见的需求为例:创建一个
[hello name="某人"]
<pre class="brush:php;toolbar:false;"><?php
require 'vendor/autoload.php'; // 引入 Composer 自动加载文件
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Processor\Processor;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Thunder\Shortcode\ShortcodeFacade; // 推荐使用 Facade 简化操作
// 1. 定义处理器:告诉库如何处理 'hello' 短代码
$handlers = new HandlerContainer();
$handlers->add('hello', function (ShortcodeInterface $s) {
// 从短代码对象中获取 'name' 参数
$name = $s->getParameter('name');
return sprintf('Hello, %s!', htmlspecialchars($name)); // 注意对输出进行转义,防止XSS
});
// 2. 创建处理器实例,并传入解析器和处理器集合
// 推荐使用 ShortcodeFacade,它封装了常用配置,更易用
$facade = new ShortcodeFacade();
$facade->addHandler('hello', function (ShortcodeInterface $s) {
$name = $s->getParameter('name');
return sprintf('Hello, %s!', htmlspecialchars($name));
});
// 3. 处理包含短代码的文本
$text = '
<div class="greeting">[hello name="Thomas"]</div>
<p>这是一段普通文本。</p>
<div class="greeting">[hello name="Peter"]</div>
<p>欢迎来到我的网站!</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/%E4%BB%A3%E7%A0%81%E5%B0%8F%E6%B5%A3%E7%86%8A">
<img src="https://img.php.cn/upload/ai_manual/001/246/273/68b6cdbf48df2598.png" alt="代码小浣熊">
</a>
<div class="aritcle_card_info">
<a href="/ai/%E4%BB%A3%E7%A0%81%E5%B0%8F%E6%B5%A3%E7%86%8A">代码小浣熊</a>
<p>代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="代码小浣熊">
<span>51</span>
</div>
</div>
<a href="/ai/%E4%BB%A3%E7%A0%81%E5%B0%8F%E6%B5%A3%E7%86%8A" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="代码小浣熊">
</a>
</div>
';
echo $facade->process($text);
/*
输出结果:
<div class="greeting">Hello, Thomas!</div>
<p>这是一段普通文本。</p>
<div class="greeting">Hello, Peter!</div>
<p>欢迎来到我的网站!</p>
*/通过这个简单的例子,你可以看到,我们不再需要编写复杂的正则表达式来匹配
[hello]
name
thunderer/shortcode
handler
thunderer/shortcode
[[tag]]
[tag]
UrlHandler
[url]
EmailHandler
WrapHandler
[b]
[i]
[raw]
RegularParser
RegexParser
WordpressParser
thunderer/shortcode
实际应用效果:
引入
thunderer/shortcode
总而言之,如果你正在处理或计划在PHP项目中引入短代码(BBCode)功能,那么
thunderer/shortcode
以上就是告别繁琐的短代码解析:如何使用thunderer/shortcode轻松构建强大的富文本功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号