public static function matchAt($regex, $string, $offset = 0)
{
$matches = [];
$string = mb_substr($string, $offset, null, 'utf-8');
if (!preg_match($regex, $string, $matches, PREG_OFFSET_CAPTURE)) {
return;
}
// PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
$charPos = mb_strlen(mb_strcut($string, 0, $matches[0][1], 'utf-8'), 'utf-8');
return $offset + $charPos;
}