PHPackages                             seiler/shortcoder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. seiler/shortcoder

AbandonedArchivedLibrary

seiler/shortcoder
=================

Build your own shortcodes system with ease

0.1.0(10y ago)41032MITPHPPHP &gt;=5.5

Since Mar 21Pushed 6y ago2 watchersCompare

[ Source](https://github.com/fredericseiler/shortcoder)[ Packagist](https://packagist.org/packages/seiler/shortcoder)[ Docs](https://github.com/fredericseiler/shortcoder)[ RSS](/packages/seiler-shortcoder/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

Shortcoder
==========

[](#shortcoder)

[![Latest Version](https://camo.githubusercontent.com/d110000926d17059dacac909b8d351614dd155838f84221b8fe7e9434684f4ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7365696c65722f73686f7274636f6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/seiler/shortcoder)[![Software License](https://camo.githubusercontent.com/39b6fddfbac4015e616f32cb1d07dbdc3809b266fee57ef9a2e8487e495d23d3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7365696c65722f73686f7274636f6465722e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/b53256434798313ae97057281dbb7fe04bf2a862d47e29263298e115603ac72c/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f66726564657269637365696c65722f73686f7274636f6465722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/fredericseiler/shortcoder)[![Coverage Status](https://camo.githubusercontent.com/adb38dad6165e8f51da1c4967eba86dbc4362404f8bd5802a1d3292fcf959a96/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f66726564657269637365696c65722f73686f7274636f6465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/fredericseiler/shortcoder/code-structure)[![Quality Score](https://camo.githubusercontent.com/8088ae009fb109329ec7a81182afd38d4bd3f276a49f1293cae0ac80a888f127/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f66726564657269637365696c65722f73686f7274636f6465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/fredericseiler/shortcoder)

Shortcoder helps you to build your own shortcodes system in your PHP application with ease.

This package is compliant with [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md), [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) and [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md). If you notice compliance oversights, please send a patch via pull request.

Requirements
------------

[](#requirements)

The following versions of PHP are supported by this version.

- PHP 5.5
- PHP 5.6
- PHP 7.0
- HHVM

Installation
------------

[](#installation)

Via Composer

```
$ composer require seiler/shortcoder
```

Usage
-----

[](#usage)

1. Create a new Shortcoder instance:

    ```
    use Seiler\Shortcoder\Shortcoder;

    $shortcoder = new Shortcoder();
    ```
2. Add your shortcodes:

    ```
    $shortcoder->add('[i]*[/i]', '*');

    $shortcoder->add('[b]*[/b]', '*');
    ```
3. Parse some text:

    ```
    echo $shortcoder->parse('I [i]love it[/i] when a plan [b]comes together[/b].');

    // I love it when a plan comes together.
    ```

Documentation
-------------

[](#documentation)

A shortcode is defined by a pattern, a replacement and an [optional regex flag](#regular-expressions). A shortcode pattern usually contains one or more wildcards to represent the content to be preserved between the pattern and the replacement.

Shortcoder allows you to `add()` shortcodes with the following syntaxes.

- Method arguments:

    ```
    $pattern = '[b]*[/b]';
    $replacement = '*';

    $shortcoder->add($pattern, $replacement);
    ```
- Key/value array:

    ```
    $shortcode = [
        '[b]*[/b]' => '*'
    ];

    $shortcoder->add($shortcode);
    ```
- Descriptive array:

    ```
    $shortcode = [
        'pattern'     => '[b]*[/b]',
        'replacement' => '*'
    ];

    $shortcoder->add($shortcode);
    ```
- Multiple shortcodes at once:

    ```
    $shortcodes = [
        [
            'pattern'     => '[b]*[/b]',
            'replacement' => '*'
        ],
        [
            'pattern'     => '[img *]',
            'replacement' => ''
        ],
        [
            '[i]*[/i]' => '*'
        ]
    ];

    $shortcoder->add($shortcodes);
    ```

The `flush()` and `set()` methods are useful for flushing the current shortcodes stack. The `set()` method also adds new shortcodes after a flush.

```
$shortcoder->set('[i]*[/i]', '*');

// is equivalent to:

$shortcoder->flush();
$shortcoder->add('[i]*[/i]', '*');
```

Because `set()` is called when you create a new Shortcoder instance, all of the `add()` syntaxes are available in the constructor as well.

```
$shortcoder = new Shortcoder($pattern, $replacement);

// is equivalent to:

$shortcoder = new Shortcoder();
$shortcoder->set($pattern, $replacement);
```

The `parse()` method takes any string as input and parses it with the stacked shortcodes.

```
$string = 'Lorem ipsum dolor sit amet';

echo $shortcoder->parse($string);
```

### Method chaining

[](#method-chaining)

The `add()`, `set()` and `flush()` methods supports method chaining.

```
echo $shortcoder->set('foo', 'bar')
                ->add($more)
                ->parse($text);
```

### Multiple wildcards

[](#multiple-wildcards)

Wildcards are replaced by regular expression powered catch-alls when adding shortcodes to the stack. It means that when you add multiple wildcards, Shortcoder will match each wildcard in a pattern to its corresponding position in the replacement.

When a wildcard directly follows another wildcard in a pattern, only the first word of the matching expression will be assigned to the first wildcard, the remaining of the expression will be catched as the second wildcard.

```
$pattern = '[alert * *]';
$replacement = '*';

$shortcoder->add($pattern, $replacement);

echo $shortcoder->parse('[alert danger This is important!]');
// This is important!
```

### Regular expressions

[](#regular-expressions)

When setting the third argument of the `add()` method (or the `regex` attribute of the shortcode) to any 'true' value, Shortcoder will handle pattern and replacement as raw regular expressions, which can be useful for more advanced usages.

```
$pattern = '/(?add($pattern, $replacement);

echo $shortcoder->parse('first then second');
// second then first
```

### Markdown compatibility

[](#markdown-compatibility)

In case you're using Shortcoder to render some HTML blocks, just append `markdown=1` in your replacement attributes to support [Markdown Extra](https://michelf.ca/projects/php-markdown/extra). Here's an example with [Parsedown Extra](https://github.com/erusev/parsedown-extra):

```
$shortcoder->add('[info *]', '*');

$text = $shortcoder->parse('[info You can use *markdown* in HTML elements.]');

echo $parsedownExtra->text($text);
//
// You can use markdown in HTML elements.
//
```

With [CommonMark](http://commonmark.org), it's a little bit [more tricky](http://spec.commonmark.org/0.24/#html-blocks). Rules are not the same for inline and block level elements. Example of an [inline level](http://spec.commonmark.org/0.24/#example-130) element with the [league/commonmark](http://commonmark.thephpleague.com) implementation:

```
$shortcoder->add('[info *]', '*');

$text = $shortcoder->parse('[info You can use *markdown* in inline HTML elements.]');

echo $commonMark->convertToHtml($text);
// You can use markdown in inline HTML elements.
```

And here's the same example, but this time with a [block level](http://spec.commonmark.org/0.24/#example-149) element:

```
$shortcoder->add('[info *]', '*');

$text = $shortcoder->parse('[info

You can use *markdown* in block HTML elements.

]');

echo $commonMark->convertToHtml($text);
//
// You can use markdown in block HTML elements.
//
```

Testing
-------

[](#testing)

```
$ phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Frederic Seiler](https://github.com/fredericseiler)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3707d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/599392adc5475e77f11251fce03c65d898883d314cda71137478f8e327fa8144?d=identicon)[fredericseiler](/maintainers/fredericseiler)

---

Top Contributors

[![fredericseiler](https://avatars.githubusercontent.com/u/954168?v=4)](https://github.com/fredericseiler "fredericseiler (1 commits)")

---

Tags

shortcodesseilershortcoder

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/seiler-shortcoder/health.svg)

```
[![Health](https://phpackages.com/badges/seiler-shortcoder/health.svg)](https://phpackages.com/packages/seiler-shortcoder)
```

###  Alternatives

[s9e/text-formatter

Multi-purpose text formatting and markup library. Plugins offer support for BBCodes, Markdown, emoticons, HTML, embedding third-party media (YouTube, etc...), enhanced typography and more.

2413.1M29](/packages/s9e-text-formatter)[webwizo/laravel-shortcodes

Wordpress like shortcodes for Laravel 5, 6, 7, 8, 9, 10, 11 and 12

216658.5k6](/packages/webwizo-laravel-shortcodes)[gornymedia/laravel-shortcodes

Laravel shortcodes package

2339.5k](/packages/gornymedia-laravel-shortcodes)[tehwave/laravel-shortcodes

Simple, elegant WordPress-like Shortcodes the Laravel way

1274.3k](/packages/tehwave-laravel-shortcodes)[vedmant/laravel-shortcodes

Wordress like shortcodes for Laravel

2811.2k2](/packages/vedmant-laravel-shortcodes)[jtsternberg/shortcode-button

Tinymce and Quicktag buttons (and modals) for outputting shortcodes. Built to work with CMB2.

781.3k1](/packages/jtsternberg-shortcode-button)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
