3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Twig { const TWIG_PREFIX = 1; const SHORT = 2; public static function getMode(string $input): int { $mode = 0; if (str_contains($input, '<twig:')) { $mode |= Twig::TWIG_PREFIX; } if (preg_match_all('/<([A-Z][a-zA-Z0-9_:-]+)([^>]*)>/', $input, $matches, \PREG_SET_ORDER)) { $mode |= Twig::SHORT; } return $mode; } } // Both $input = '<twig:test></twig:test> <Test>test</Test>'; $mode = Twig::getMode($input); var_dump($mode); $hasTwigPrefix = (bool) ($mode & Twig::TWIG_PREFIX); $hasShort = (bool) ($mode & Twig::SHORT); var_dump($hasTwigPrefix); var_dump($hasShort); // Twig $input = '<twig:test></twig:test>'; $mode = Twig::getMode($input); var_dump($mode); $hasTwigPrefix = (bool) ($mode & Twig::TWIG_PREFIX); $hasShort = (bool) ($mode & Twig::SHORT); var_dump($hasTwigPrefix); var_dump($hasShort); // Short $input = '<Test>test</Test>'; $mode = Twig::getMode($input); var_dump($mode); $hasTwigPrefix = (bool) ($mode & Twig::TWIG_PREFIX); $hasShort = (bool) ($mode & Twig::SHORT); var_dump($hasTwigPrefix); var_dump($hasShort); // Nothing $input = '<section>test</section>'; $mode = Twig::getMode($input); var_dump($mode); $hasTwigPrefix = (bool) ($mode & Twig::TWIG_PREFIX); $hasShort = (bool) ($mode & Twig::SHORT); var_dump($hasTwigPrefix); var_dump($hasShort);

preferences:
28.44 ms | 406 KiB | 5 Q