Alias HTML elements to other tags

Usually, HTML is meant to be presented to the user as-is. However, sometimes you may want to alias HTML elements and HTML attributes to other tags and attributes. Here is a concrete example: let's say you want to limit the number of links to 3 per text but users can post links many different ways via the [url] BBCode, as an <a href> element or even in plain text, letting the Autolink plugin linkify them. Your solution is to use the same tag for the 3 different plugins.

In the following example, we configure the HTMLElements plugin to alias <a href> to use the same tag as the BBCodes and Autolink plugins. Then we see that despite using 3 different syntaxes, only the first 3 URLs are linked.

$configurator = new s9e\TextFormatter\Configurator;

// The [url] BBCode uses a tag named "URL" with an attribute of the same name

// <a> will use the "URL" tag
$configurator->HTMLElements->aliasElement('a', 'URL');

// The "href" attribute will use the "url" attribute
$configurator->HTMLElements->aliasAttribute('a', 'href', 'url');

// Limit the number of URL tags to 3
$configurator->tags['URL']->tagLimit = 3;

// Get an instance of the parser and the renderer

$text = '
[url=]Second link[/url]
<a href="">The third and last</a>
<a href="">This one will not be linkified</a>';

$xml  = $parser->parse($text);
$html = $renderer->render($xml);

echo $html;
<a href=""></a>
<a href="">Second link</a>
<a href="">The third and last</a>
&lt;a href=""&gt;This one will not be linkified&lt;/a&gt;