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
入门 *** (WIP) ***
让我们来看一个简单的例子。我们想要为 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); } }