Overview
KB
Technical FAQ
PHP Manual
CSS2 Manual
HTML Manual
JS Guide
JS Reference
PhpDock Manual
Nu-Coder Manual
PhpExpress Manual
PHP Joomla
Development
Learn PHP
 
<mb_decode_mimeheadermb_detect_encoding>
^Multibyte String FunctionsLast updated: Mon, 26 Oct 2026

mb_decode_numericentity

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_decode_numericentity — Decode HTML numeric string reference to character

Description

function mb_decode_numericentity(string $string, array $map, ?string $encoding = null): string

Convert numeric string reference of string string in a specified block to character.

Parameters

string

The string being decoded.

map

map is an array that specifies the code area to convert.

encoding

The encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return Values

The converted string.

Errors/Exceptions

Throws a ValueError if map contains any value that is not an int, float, bool, null, or a numeric string.

As of PHP 8.0.0, a ValueError is thrown if encoding is an invalid encoding. Prior to PHP 8.0.0, an E_WARNING was emitted instead.

Changelog

Version Description
8.4.0 The mb_decode_numericentity() function now throws a ValueError if map contains any value that cannot be implicitly converted to int.
8.0.0 encoding is nullable now.
8.0.0 Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned.

Examples

Example #1 map example

<?php

$convmap = array(
    int start_code1, int end_code1, int offset1, int mask1,
    int start_code2, int end_code2, int offset2, int mask2,
    // ........
    int start_codeN, int end_codeN, int offsetN, int maskN
);
// Specify Unicode value for start_codeN and end_codeN
// Add offsetN to value and take bit-wise 'AND' with maskN, 
// then convert value to numeric string reference.
?>

Example #2 map example escapes JavaScript string

<?php

function escape_javascript_string($str)
{
    $map = [
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,0,0, // 49
        0,0,0,0,0,0,0,0,1,1,
        1,1,1,1,1,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,
        0,1,1,1,1,1,1,0,0,0, // 99
        0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,
        0,0,0,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1, // 149
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1, // 199
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1,
        1,1,1,1,1,1,1,1,1,1, // 249
        1,1,1,1,1,1,1, // 255
    ];

    // Char encoding is UTF-8
    $mblen = mb_strlen($str, 'UTF-8');
    $utf32 = bin2hex(mb_convert_encoding($str, 'UTF-32', 'UTF-8'));

    for ($i=0, $encoded=''; $i < $mblen; $i++) {
        $u = substr($utf32, $i * 8, 8);
        $v = base_convert($u, 16, 10);

        if ($v < 256 && $map[$v]) {
            $encoded .= '\\x' . substr($u, 6,2);
        } else if ($v == 2028) {
            $encoded .= '\\u2028';
        } else if ($v == 2029) {
            $encoded .= '\\u2029';
        } else {
            $encoded .= mb_convert_encoding(hex2bin($u), 'UTF-8', 'UTF-32');
        }
    }

    return $encoded;
}
 
// Test data
$convmap = [ 0x0, 0xffff, 0, 0xffff ];
$msg = '';

for ($i=0; $i < 1000; $i++) {
  // chr() cannot generate correct UTF-8 data larger value than 128, use mb_decode_numericentity().
  $msg .= mb_decode_numericentity('&#' . $i . ';', $convmap, 'UTF-8');
}
 
// var_dump($msg);
var_dump(escape_javascript_string($msg));
?>

See Also


<mb_decode_mimeheadermb_detect_encoding>
^Multibyte String FunctionsLast updated: Mon, 26 Oct 2026