Symfony\Polyfill\Mbstring

Mbstring::utf8Offset │ private static │ CF7 1.0

Метод класса: Mbstring{}

Хуков нет.

Возвращает

int|false. The offset in bytes of the character at $offset, false when it is out of range, like native mbstring

Использование

$result = Mbstring::utf8Offset( $s, $offset, $bytes );
$s(строка) (обязательный)
.
$offset(int) (обязательный)
.
$bytes(true|false) (обязательный)
.

Код Mbstring::utf8Offset() CF7 6.2.4.1

private static function utf8Offset(string $s, int $offset, bool $bytes)
{
    if (0 === $offset) {
        return 0;
    }

    if ($bytes || preg_match('//u', $u = str_replace("\xFF", '?', $s))) {
        $length = $bytes ? \strlen($s) : self::utf8Length($s);

        if (0 > $offset) {
            $offset += $length;
        }

        if (0 > $offset || $offset > $length) {
            return false;
        }

        if ($bytes) {
            return $offset;
        }

        for ($rx = ''; 65535 < $offset; $offset -= 65535) {
            $rx .= '.{65535}';
        }
        preg_match('/^'.$rx.'.{'.$offset.'}\K/su', $u, $m, \PREG_OFFSET_CAPTURE);

        return $m[0][1];
    }

    // Walk ill-formed UTF-8 like native mbstring: backward over the bytes that are not continuation bytes, forward by the lengths that lead bytes announce
    $length = \strlen($s);

    if (0 > $offset) {
        $i = $length;

        while (0 > $offset) {
            if (0 === $i) {
                return false;
            }

            if (0x80 !== (\ord($s[--$i]) & 0xC0)) {
                ++$offset;
            }
        }

        return $i;
    }

    $i = 0;

    while (0 < $offset--) {
        if ($i >= $length) {
            return false;
        }

        $c = \ord($s[$i]);
        $i += $c < 0xC2 ? 1 : ($c < 0xE0 ? 2 : ($c < 0xF0 ? 3 : ($c < 0xF5 ? 4 : 1)));
    }

    return min($i, $length);
}