jaybizzle / shortcodes
BBCode/WordPress 样式的短代码
v1.1.2
2022-11-16 15:21 UTC
Requires (Dev)
- phpunit/phpunit: ^4.8|^5.5|^6.5
- satooshi/php-coveralls: 1.*
This package is auto-updated.
Last update: 2024-09-16 19:11:38 UTC
README
短代码
Shortcodes 是一个 PHP 库,它可以帮助解析 WordPress/BBCode 样式的短代码。它可以将以下内容...
[style color=#FF0000]Red Text[/style]
转换成以下内容...
<span style="color:#FF0000;">Red Text</span>
输出不是预定义的,如何处理输出取决于您。以下是一些示例。
安装
composer require jaybizzle/shortcodes
入门 *** (进行中) ***
让我们举一个简单的例子。我们想要为 video
元素创建一个短代码。我们希望可以编写如下内容...
[video title="My Awesome Video" videoID=345 width=320 height=240]
并使其输出如下内容...
<video width="320" height="240" controls> <source src="/videos/video-345.mp4" type="video/mp4"> <source src="/videos/video-345.ogg" type="video/ogg"> Your browser does not support the video tag. </video>
首先,我们需要创建一个类来处理解析后的短代码及其属性。我们可以创建一个新的类,如下所示...
<?php namespace App\Shortcodes; use Jaybizzle\Shortcodes\Shortcode; class VideoShortcode extends Shortcode { public static $shortcode = 'video'; public function parse() { // All shortcode attributes will be available in $this->attr // i.e. given the example above... // $this->attr['title'] // $this->attr['videoID'] // $this->attr['width'] // $this->attr['height'] } }
接下来,我们需要将此短代码解析类添加到 Shortcodes 库中,如下所示...
<?php namespace App\Libraries; use App\Shortcodes\VideoShortcode; use Jaybizzle\Shortcodes\Shortcodes; class MyClass { public function index() { $shortcodes = new Shortcodes $shortcodes->add(VideoClass::class); } }