Simple wordpress-like shortcode parser for PHP. Also parses nested shortcodes logically.
Empty shortcodes can be written in both ways: [tag] or [tag /]
Shortcode::register('week', function($attr, $content) {
return intval(date('W'));
});
$input = 'Week in this year: [week]';
echo Shortcode::process($input);
Week in this year: 42
Shortcode::register('b', function($attr, $content) {
return '<strong>'.$content.'</strong>';
});
$input = 'This text is [b]fat[/b]!';
echo Shortcode::process($input);
This text is fat!
Shortcode::register('time', function($attr, $content) {
$attr = array_replace([
'format' => 'H:i:s'
], $attr);
return date($attr['format']);
});
$input = 'It\'s [time format="g" /] o\'clock!';
echo Shortcode::process($input);
It's 12 o'clock!
Shortcode::register('row', function($attr, $content) {
return '<div class="row">'.$content.'</div>';
});
Shortcode::register('col', function($attr, $content) {
$attr = array_replace([
'width' => 12
], $attr);
return '<div class="col-sm-'.$attr['width'].'">'.$content.'</div>';
});
$input = ''
. '[row]'
. '[col width="9"]Col 1'
. '[row]'
. '[col width="6"]Col 1.1[/col]'
. '[col width="6"]Col 1.2[/col]'
. '[/row]'
. '[/col]'
. '[col width="3"]Col 2[/col]'
. '[/row]';
echo Shortcode::process($input);
<div class="row">
<div class="col-sm-9">Col 1
<div class="row">
<div class="col-sm-6">Col 1.1</div>
<div class="col-sm-6">Col 1.2</div>
</div>
</div>
<div class="col-sm-3">Col 2</div>
</div>