s9e/text-formatter
PHP text formatting library with plugin support for BBCode, Markdown, HTML, and more. Includes predefined bundles, extensive documentation, and a JavaScript port for client-side preview and demos. Install via Composer and integrate customizable parsing/rendering.
By default, an attribute filter or a tag filter only receives one argument: the attribute's value or the tag, respectively. Additional parameters can be appended with the methods addParameterByName() and addParameterByValue() and the whole list of parameters can be cleared with resetParameters(). Variables set in $configurator->registeredVars are available by name and can be changed at parsing time via $parser->registeredVars. Other special parameters listed below are available by name.
1 This parameter is skipped in JavaScript filters.
2 This parameter is subject to change and may be removed in a future version.
3 Available since 2.0.0.
In this example, we create a BBCode that displays the 8 first characters of a given string. The value is static, therefore addParameterByValue() is used.
$configurator = new s9e\TextFormatter\Configurator;
// Create a [X] BBCode to test our filter
$configurator->BBCodes->addCustom('[X string={TEXT}]', '{[@string](https://github.com/string)}');
// Add our custom filter to this attribute. It's equivalent to calling
// substr($attrValue, 0, 8)
$filter = $configurator->tags['X']->attributes['string']->filterChain->append('substr');
$filter->addParameterByValue(0);
$filter->addParameterByValue(8);
// Get an instance of the parser and the renderer
extract($configurator->finalize());
$text = '[X="1234567890"]';
$xml = $parser->parse($text);
$html = $renderer->render($xml);
echo $html;
12345678
Now let's modify this example to make the length variable. We create a variable named myLength in the configurator with a default value of 8 and use it as a by-name parameter. This variable can be changed at parsing time.
$configurator = new s9e\TextFormatter\Configurator;
// Create a myLength variable in the configurator
$configurator->registeredVars['myLength'] = 8;
// Create a [X] BBCode to test our filter
$configurator->BBCodes->addCustom('[X string={TEXT}]', '{[@string](https://github.com/string)}');
// Add our custom filter to this attribute. It's equivalent to calling
// substr($attrValue, 0, $myLength)
$filter = $configurator->tags['X']->attributes['string']->filterChain->append('substr');
$filter->addParameterByValue(0);
$filter->addParameterByName('myLength');
// Get an instance of the parser and the renderer
extract($configurator->finalize());
$text = '[X="1234567890"]';
$xml = $parser->parse($text);
$html = $renderer->render($xml);
echo $html, "\n";
// Change the value at parsing time and try again
$parser->registeredVars['myLength'] = 4;
$text = '[X="1234567890"]';
$xml = $parser->parse($text);
$html = $renderer->render($xml);
echo $html;
12345678
1234
In addition to the verbose API, the callback signature can be specified within parentheses using a syntax similar to a subset of PHP. The supported parameter types are:
$attrValue123, 'string' or "double\nstring"true, false or null['foo' => 1, 'bar' => 2]/^foo$/iThe regexp notation exists for compatibility with JavaScript. They are automatically cast as strings in PHP or as RegExp literals in JavaScript. If you don't use JavaScript, you can simply use strings.
$configurator = new s9e\TextFormatter\Configurator;
$configurator->BBCodes->addCustom('[X string={TEXT}]', '{[@string](https://github.com/string)}');
$configurator->tags['X']->attributes['string']->filterChain
->append('str_replace("foo", "bar", $attrValue)');
// This is the the same the following:
//
//$configurator->tags['X']->attributes['string']->filterChain
// ->append('str_replace')
// ->resetParameters()
// ->addParameterByValue('foo')
// ->addParameterByValue('bar')
// ->addParameterByName('attrValue');
// Get an instance of the parser and the renderer
extract($configurator->finalize());
$text = '[X="foo bar baz"]';
$xml = $parser->parse($text);
$html = $renderer->render($xml);
echo $html;
bar bar baz
How can I help you explore Laravel packages today?