- var_dump: documentation ( source)
- preg_match_all: documentation ( source)
- str_contains: documentation ( source)
<?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);