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 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

// Add our custom filter to this attribute. We also need to declare the second
// argument that our callback expects

// Get an instance of the parser and the renderer

$text = '[img][/img]';
$xml  = $parser->parse($text);
$html = $renderer->render($xml);

echo $html;
<img src="" title="" alt="">