Symfony\Polyfill\Mbstring

Mbstring::findPart │ private static │ CF7 1.0

Returns the part of $haystack before or from $needle like native mbstring.

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

Хуков нет.

Возвращает

string|false.

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

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

Код Mbstring::findPart() CF7 6.2.4.1

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

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

        return false;
    }

    $haystack = (string) $haystack;

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

    [$h, $n, $bytes] = $search;

    if (false === $pos = $reverse ? strrpos($h, $n) : strpos($h, $n)) {
        return false;
    }

    if ($bytes) {
        return (string) ($part ? substr($haystack, 0, $pos) : substr($haystack, $pos));
    }

    if ($fold || ('UTF-8' === $encoding && (!preg_match('//u', $haystack) || !preg_match('//u', $needle)))) {
        // Cut after as many characters as native mbstring counts before the match
        $pos = self::utf8Length(substr($h, 0, $pos));
        $h = 'UTF-8' === $encoding ? self::markIllFormedUtf8($haystack) : self::utf8($haystack, $encoding);
        $pos = self::utf8Offset($h, $pos, false);
    }

    $h = (string) ($part ? substr($h, 0, $pos) : substr($h, $pos));

    if (false !== strpos($h, "\xFF")) {
        $h = str_replace("\xFF", 'none' === mb_substitute_character() ? '' : '?', $h);
    }

    return 'UTF-8' === $encoding ? $h : (string) @self::iconv('UTF-8', $encoding, $h);
}