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

As a string

$configurator = new s9e\TextFormatter\Configurator;
$configurator->BBCodes->addFromRepository('URL');

// Add a target="_blank" attribute to the URL tag's template using
// a pattern replacement
$tag = $configurator->tags['URL'];
$tag->template = preg_replace(
	'(<a\\b(?![^>]*target))',
	'<a target="_blank"',
	$tag->template
);

extract($configurator->finalize());

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

echo $html;
<a target="_blank" href="http://example.org" rel="noreferrer">http://example.org</a>

As a DOM tree

$configurator = new s9e\TextFormatter\Configurator;
$configurator->BBCodes->addFromRepository('URL');

// Get the default URL template as a DOMDocument
$dom = $configurator->tags['URL']->template->asDOM();

// Set a target="_blank" attribute to any <a> element
foreach ($dom->getElementsByTagName('a') as $a)
{
	$a->setAttribute('target', '_blank');
}

// Save the changes
$dom->saveChanges();

// Test the new template
extract($configurator->finalize());

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

echo $html;
<a href="http://example.org" target="_blank" rel="noreferrer">http://example.org</a>

Starting with version 2.15.0, DOM manipulation is enhanced with the SweetDOM 3.0 API. In the following example, we showcase two different ways to achieve the same result: add an attribute to a elements if a given parameter is set. In one case we add target="_blank" if $NEWTAB is set, and in the other we set rel="ugc" if $UGC is set.

$configurator = new s9e\TextFormatter\Configurator;
$configurator->BBCodes->addFromRepository('URL');

// Modify the template
$dom = $configurator->tags['URL']->template->asDOM();
foreach ($dom->getElementsByTagName('a') as $a)
{
	$a->prependXslIf(test: '$NEWTAB')
	  ->appendXslAttribute(name: 'target', textContent: '_blank');

	$a->prependXslIf('$UGC')->appendXslAttribute('rel', 'ugc');
}
$dom->saveChanges();

// Test the new template
extract($configurator->finalize());

$text = '[url]http://example.org[/url]';
$xml  = $parser->parse($text);

$renderer->setParameter('NEWTAB', '');
$renderer->setParameter('UGC',    '');
echo $renderer->render($xml), "\n";

$renderer->setParameter('NEWTAB', '1');
$renderer->setParameter('UGC',    '1');
echo $renderer->render($xml);
<a href="http://example.org">http://example.org</a>
<a href="http://example.org" rel="ugc" target="_blank">http://example.org</a>
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.
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
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours