[Entwickler] Shunting-yard

  • Diese Erweiterung für die WoltLab Suite 5.2+ erweitert selbige um eine Implementation des Shunting-yard-Algorithmus, mit dessen Hilfe sich mathematische Operationen (z.B. aus einer Option heraus) ohne Nutzung potenziell gefährlicher PHP-Funktionen ausführen lassen.


    Nutzungsbeispiele:


    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']; // erlaubte PHP-Funktionen@@@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

    Alles anzeigen


    Zur Original Nachricht: https://pluginstore.woltlab.com/file/7071-entw…-shunting-yard/