Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Text Formatter Laravel Package

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.

View on GitHub
Deep Wiki
Context7

How attribute filters work

Attribute filters are callbacks that can be used to validate or sanitize user input. During parsing, they are called for each attribute using the attribute's value as sole argument and their return value replaces the attribute's value. If the return value is false then the attribute is declared invalid and no more filter is applied.

Add a default attribute filter

The library provides a built-in collection of attribute filters which cover most of the common cases. In the following example, we create TEXTSIZE BBCode with a size attribute, to which we add a #uint filter to ensure that its value is a positive integer.

$configurator = new s9e\TextFormatter\Configurator;

// Create the TEXTSIZE BBCode/tag
$bbcode = $configurator->BBCodes->add('TEXTSIZE');
$tag    = $configurator->tags->add('TEXTSIZE');
$tag->template = '<span style="font-size:{[@size](https://github.com/size)}%"><xsl:apply-templates/></span>';

// Create the size attribute
$attribute = $tag->attributes->add('size');
$attribute->filterChain->append('#uint');

extract($configurator->finalize());

$text = '[TEXTSIZE size=1000]This is probably too big[/TEXTSIZE]';
$xml  = $parser->parse($text);
$html = $renderer->render($xml);

echo $html;
<span style="font-size:1000%">This is probably too big</span>

Some built-in filters may accept arguments. In the following example, instead of using a #uint filter, we use a #range filter to limit the size between 50 and 500 using the syntax #range(50, 500).

$configurator = new s9e\TextFormatter\Configurator;

// Create the TEXTSIZE BBCode/tag
$bbcode = $configurator->BBCodes->add('TEXTSIZE');
$tag    = $configurator->tags->add('TEXTSIZE');
$tag->template = '<span style="font-size:{[@size](https://github.com/size)}%"><xsl:apply-templates/></span>';

// Create the size attribute
$attribute = $tag->attributes->add('size');
$attribute->filterChain->append('#range(5, 500)');

extract($configurator->finalize());

$text = '[TEXTSIZE size=1000]This is still pretty big[/TEXTSIZE]';
$xml  = $parser->parse($text);
$html = $renderer->render($xml);

echo $html;
<span style="font-size:500%">This is still pretty big</span>

Add a custom attribute filter

In the following example, we create an [IMG] BBCode and we add a filter on the src attribute to replace the URL with our own.

$configurator = new s9e\TextFormatter\Configurator;

function replaceUrl($attrValue, $proxyUrl)
{
	return $proxyUrl . urlencode($attrValue);
}

// Add the default [IMG] BBCode
$configurator->BBCodes->addFromRepository('IMG');

// Add our custom filter to this attribute. We also need to declare the second
// argument that our callback expects
$configurator->tags['IMG']->attributes['src']->filterChain
	->append('replaceUrl')
	->addParameterByValue('http://example.org/proxy.php?');

// Get an instance of the parser and the renderer
extract($configurator->finalize());

$text = '[img]http://example.com/img.png[/img]';
$xml  = $parser->parse($text);
$html = $renderer->render($xml);

echo $html;
<img src="http://example.org/proxy.php?http%3A%2F%2Fexample.com%2Fimg.png" title="" alt="">
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver