PHP code example of seiler / shortcoder

1. Go to this page and download the library: Download seiler/shortcoder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

seiler / shortcoder example snippets


    use Seiler\Shortcoder\Shortcoder;

    $shortcoder = new Shortcoder();
    

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

    $shortcoder->add('[b]*[/b]', '<strong>*</strong>');
    

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

    // I <em>love it</em> when a plan <strong>comes together</strong>.
    

    $pattern = '[b]*[/b]';
    $replacement = '<strong>*</strong>';

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

    $shortcode = [
        '[b]*[/b]' => '<strong>*</strong>'
    ];

    $shortcoder->add($shortcode);
    

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

    $shortcoder->add($shortcode);
    

    $shortcodes = [
        [
            'pattern'     => '[b]*[/b]',
            'replacement' => '<strong>*</strong>'
        ],
        [
            'pattern'     => '[img *]',
            'replacement' => '<img class="img-responsive" src="*">'
        ],
        [
            '[i]*[/i]' => '<em>*</em>'
        ]
    ];

    $shortcoder->add($shortcodes);
    

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

// is equivalent to:

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

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

// is equivalent to:

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

$string = 'Lorem ipsum dolor sit amet';

echo $shortcoder->parse($string);

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

$pattern = '[alert * *]';
$replacement = '<div class="alert alert-*">*</div>';

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

echo $shortcoder->parse('[alert danger This is important!]');
// <div class="alert alert-danger">This is important!</div>

$pattern = '/(?<=^|\s)@(\w{1,15})/m'; // Twitter @handle
$replacement = '<a href="https://twitter.com/$1">@$1</a>';

// setting the third argument
$shortcoder->set($pattern, $replacement, true);

// or setting the 'regex' attribute
$shortcoder->set([
    'pattern' => $pattern,
    'replacement' => $replacement,
    'regex' => true
]);

echo $shortcoder->parse('Do you follow @php_net ?');
// Do you follow <a href="https://twitter.com/php_net">@php_net</a> ?

$pattern = '* then *';
$replacement = '$2 then *';

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

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

$shortcoder->add('[info *]', '<div class="info" markdown=1>*</div>');

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

echo $parsedownExtra->text($text);
// <div class="info">
// <p>You can use <em>markdown</em> in HTML elements.</p>
// </div>

$shortcoder->add('[info *]', '<span class="info">*</span>');

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

echo $commonMark->convertToHtml($text);
// <p><span class="info">You can use <em>markdown</em> in inline HTML elements.</span></p>

$shortcoder->add('[info *]', '<div class="info">*</div>');

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

You can use *markdown* in block HTML elements.

]');

echo $commonMark->convertToHtml($text);
// <div class="info">
// <p>You can use <em>markdown</em> in block HTML elements.</p>
// </div>