3v4l.org

run code in 300+ PHP versions simultaneously
<?php #BLM final class ABNFParser { private const ABNF_REGEX = <<<'REGEX' (?(DEFINE) (?<ALPHA>[A-Za-z]) (?<DIGIT>[0-9]) (?<SP>[\x20]) (?<WSP>[(?&SP)\x09]) (?<CR>\x0D) (?<LF>\x0A) (?<Repeat>(?:(?:(?&DIGIT)+(?:(?&DIGIT)*[\x2A](?&DIGIT)*)))) ) REGEX; public function parse(string $input, string $rule): ?string { // Build the query pattern dynamically based on the provided rule $pattern = sprintf('#%s(?&%s)#Aux', self::ABNF_REGEX, $rule); if (preg_match($pattern, $input, $matches)) { return $matches[0]; } return null; } } # Example Usage $parser = new ABNFParser(); $input = '2*4'; $rule = 'Repeat'; $parsedResult = $parser->parse($input, $rule); var_dump($parsedResult); // Outputs: string(3) "2*4"
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
string(3) "2*4"

preferences:
63.7 ms | 406 KiB | 5 Q