s9e / TextFormatter

Text formatting library that supports BBCode, HTML and other markup via plugins. Handles emoticons, censors words, automatically embeds media and more.
MIT License
233 stars 36 forks source link

How can I use a filter (token) for values like 100%? #209

Closed Aurorum-Studio closed 1 year ago

Aurorum-Studio commented 1 year ago

Hello, I want to define a BBcode like

'[iframe={URL} width={XXX}]'

, and I want the value of "width" to be able to be a percentage, like 100%. However, I tried all the "build-in filters" from the document, but none of them allow a "%" to occur, how could I achieve this? Thank you very much.

The document I read. https://s9etextformatter.readthedocs.io/Filters/Built-in_filters/

JoshyPHP commented 1 year ago

You can use a #regexp filter with a pattern that fits your exact specifications. For example:

$configurator = new s9e\TextFormatter\Configurator;
$configurator->BBCodes->addCustom(
    '[iframe url={URL} width={REGEXP=/^[0-9]+%$/}]',
    ''
);

extract($configurator->finalize());

$text = '[iframe=... width=100%]';
$xml  = $parser->parse($text);

die("$xml\n");
<r><IFRAME url="..." width="100%">[iframe=... width=100%]</IFRAME></r>
Aurorum-Studio commented 1 year ago

emm, I meant that I need to add a percentage (not a specific one), while REGEXP=/^[0-9]+%$/ use a value that is preset (I think it means this, if I am wrong, please tell me), it could be any percentage entered by the user, my current extension.php looks like this, I just want to change the type of value that can be entered in the "width" attribute 😅.

use s9e\TextFormatter\Configurator; 

return [ 
  (new Extend\Frontend('forum'))
  ->css(__DIR__.'/less/forum.less'),
    (new Extend\Formatter)
    ->configure(function (Configurator $config) {
         $config->BBCodes->addCustom(
          '[iframe={URL} width={INT1} height={INT2} frameborder={INT3} marginwidth={INT4} marginheight={INT5} scrolling={SIMPLETEXT2} allowtransparency={SIMPLETEXT1}]',
           '<div class="iframe">
           <iframe 
             src="{URL}"
             width="{INT1}"
             height="{INT2}"
             frameborder="{INT3}"
             marginwidth="{INT4}"
             marginheight="{INT5}"
             vspace="0"
             hspace="0"
             allowtransparency="{SIMPLETEXT1}"
             scrolling="{SIMPLETEXT2}"
             allowfullscreen="true"
           >
           </iframe>
         </div>'
        );
    })
];`
JoshyPHP commented 1 year ago

You'd need to check out the regexp syntax. ^[0-9]+%$ means that it starts with 1 or more digits between 0-9, followed by % at the end. It accepts 0% or 123%. If the % is optional you have to add ?, which gives you ^[0-9]+%?$

https://regexr.com/772nl