Hei ! Guest  

[Developer] Shunting-yard

  • This extension for WoltLab Suite 5.2+ extends the same with an implementation of the shunting-yard algorithm, which can be used to perform mathematical operations (e.g. from an option) without using potentially dangerous PHP functions.

    Usage examples:

    PHP

    Code
    require_once(WCF_DIR . 'lib/system/api/php-shunting-yard/autoload.php');@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@$equation = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3';@@@WCF_PRE_LINEBREAK@@@$result = \RR\Shunt\Parser::parse($equation);@@@WCF_PRE_LINEBREAK@@@echo $result; // 3.0001220703125

    PHP

    Code
    require_once(WCF_DIR . 'lib/system/api/php-shunting-yard/autoload.php');@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@$string = 'max(%x * 0.1 + 0.35, 2000)';@@@WCF_PRE_LINEBREAK@@@$string = str_replace('%x', 3434.133, $string);@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@$ctx = new \RR\Shunt\Context();@@@WCF_PRE_LINEBREAK@@@$allowedFunctions = ['ceil', 'exp', 'floor', 'fmod', 'max', 'min', 'pow']; // allowed PHP functions@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@foreach ($allowedFunctions as $fn) {@@@WCF_PRE_LINEBREAK@@@    if (str_contains($string, $fn . '(')) {@@@WCF_PRE_LINEBREAK@@@        $ctx->def($fn);@@@WCF_PRE_LINEBREAK@@@    }@@@WCF_PRE_LINEBREAK@@@}@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@$result = \RR\Shunt\Parser::parse($string, $ctx);@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@echo $result; // 2000
    Display More

    Source: https://pluginstore.woltlab.com/file/7072-developer-shunting-yard/

  • width=

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!