Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Mbstring.php

function Mbstring::mb_encode_numericentity

File

vendor/symfony/polyfill-mbstring/Mbstring.php, line 215

Class

Mbstring
Partial mbstring implementation in PHP, iconv based, UTF-8 centric.

Namespace

Symfony\Polyfill\Mbstring

Code

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 = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
        }
    }
    else {
        $s = iconv($encoding, 'UTF-8//IGNORE', $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"];
        $uchr = substr($s, $i, $ulen);
        $i += $ulen;
        $c = self::mb_ord($uchr);
        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 iconv('UTF-8', $encoding . '//IGNORE', $result);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal