Symfony\Polyfill\Mbstring

Mbstring::find │ private static │ CF7 1.0

Finds the position of $needle in $haystack like native mbstring.

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

Хуков нет.

Возвращает

int|false.

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

$result = Mbstring::find( $function, $haystack, $needle, $offset, $encoding, $reverse, $fold );
$function(строка) (обязательный)
.
$haystack(обязательный)
.
$needle(обязательный)
.
$offset(обязательный)
.
$encoding(обязательный)
.
$reverse(true|false) (обязательный)
.
$fold(true|false) (обязательный)
.

Код Mbstring::find() CF7 6.2.4.1

private static function find(string $function, $haystack, $needle, $offset, $encoding, bool $reverse, bool $fold)
{
    $needle = (string) $needle;

    if (80000 > \PHP_VERSION_ID && '' === $needle && 'mb_stripos' === $function) {
        trigger_error($function.'(): Empty delimiter', \E_USER_WARNING);

        return false;
    }

    if (!$search = self::prepareSearch((string) $haystack, $needle, self::searchEncoding($encoding, $fold), $fold, $fold)) {
        return false;
    }

    [$haystack, $needle, $bytes, $dropped] = $search;

    if (80000 > \PHP_VERSION_ID && $fold && ('' === $haystack || '' === $needle)) {
        return false;
    }

    if (false === $start = self::utf8Offset($haystack, $offset = (int) $offset, $bytes)) {
        // Native mbstring counts the ill-formed sequences that iconv() dropped: the offset could be in range
        if ($dropped && abs($offset) <= self::utf8Length($haystack) + $dropped) {
            return false;
        }

        if (80000 > \PHP_VERSION_ID) {
            trigger_error($function.'(): '.($reverse ? 'Offset is greater than the length of haystack string' : 'Offset not contained in string'), \E_USER_WARNING);

            return false;
        }

        throw new \ValueError($function.'(): Argument #3 ($offset) must be contained in argument #1 ($haystack)');
    }

    if (80000 > \PHP_VERSION_ID && '' === $needle) {
        if ('mb_strpos' === $function) {
            trigger_error($function.'(): Empty delimiter', \E_USER_WARNING);
        }

        return false;
    }

    if (!$reverse) {
        $pos = strpos($haystack, $needle, $start);
    } else {
        $pos = strrpos($haystack, $needle, 0 > $offset ? $start - \strlen($haystack) : $start);
    }

    return false === $pos || $bytes ? $pos : self::utf8Length(substr($haystack, 0, $pos));
}