Symfony\Polyfill\Mbstring

Mbstring::mb_encode_numericentity │ public static │ CF7 1.0

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

Хуков нет.

Возвращает

null. Ничего (null).

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

$result = Mbstring::mb_encode_numericentity( $s, $convmap, $encoding, $is_hex );
$s(обязательный)
.
$convmap(обязательный)
.
$encoding
.
По умолчанию: null
$is_hex
.
По умолчанию: false

Код Mbstring::mb_encode_numericentity() CF7 6.2.4.1

public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false)
{
    if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) {
        trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', \E_USER_WARNING);

        return null;
    }

    if (!\is_array($convmap) || (80000 > \PHP_VERSION_ID && !$convmap)) {
        return false;
    }

    if (null !== $encoding && !\is_scalar($encoding)) {
        trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', \E_USER_WARNING);

        return null;  // Instead of '' (cf. mb_decode_numericentity).
    }

    if (null !== $is_hex && !\is_scalar($is_hex)) {
        trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', \E_USER_WARNING);

        return null;
    }

    $s = (string) $s;
    if ('' === $s) {
        return '';
    }

    $encoding = self::getEncoding($encoding);

    if ('UTF-8' === $encoding) {
        $encoding = null;
        if (!preg_match('//u', $s)) {
            $s = self::scrubUtf8($s);
        }
    } else {
        $s = self::iconv($encoding, 'UTF-8', $s);
    }

    static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4];

    $cnt = floor(\count($convmap) / 4) * 4;
    $i = 0;
    $len = \strlen($s);
    $result = '';

    while ($i < $len) {
        $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"] ?? 1;
        $uchr = substr($s, $i, $ulen);
        $i += $ulen;

        // code points above U+10FFFF are outside any convmap, but mb_ord() rejects them
        if (false === $c = self::mb_ord($uchr)) {
            $result .= $uchr;
            continue;
        }

        for ($j = 0; $j < $cnt; $j += 4) {
            if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) {
                $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3];
                $result .= $is_hex ? \sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';';
                continue 2;
            }
        }
        $result .= $uchr;
    }

    if (null === $encoding) {
        return $result;
    }

    return self::iconv('UTF-8', $encoding, $result);
}