Thursday, September 4, 2025
HomeLanguagesPHP mb_encode_numericentity() Function

PHP mb_encode_numericentity() Function

The mb_encode_numericentity() function is an inbuilt function in PHP that is used to convert characters in a given string to their corresponding HTML numeric entity representations.

Syntax

mb_encode_numericentity(
    string $string,
    array $map,
    ?string $encoding = null,
    bool $hex = false
): string

Parameters

This function accepts four parameters that are described below.

  • $string: This parameter specifies the string that is to be decoded.
  • $map: The code area that is to be transformed, is represented by the map, which is an array.
  • hex: This parameter denotes either the entity reference will be hexadecimal notation or decimal notation.
  • $encoding: The encoding parameter denotes the character encoding. If omitted or null, then the internal character encoding value will be used.

Return Value

The mb_encode_numericentity() function returns the encoded string if the function successfully executes otherwise. It will return false.

Program 1: The following program demonstrates the mb_encode_numericentity() function.

PHP




<?php
  
$input_string = "Bonjour tout le monde! Ça va bien ?";
  
// Check if the input string contains french characters
if (preg_match("/[\x{00C0}-\x{02AF}]/u", $input_string)) {
    $convmap = [0x80, 0x10ffff, 0, 0x7fffffff];
    
    // Encode characters from decimal
   // (hexadecimal 0x80 to 0x10FFFF)
    $encoded_string = mb_encode_numericentity(
        $input_string,
        $convmap,
        "UTF-8",
        true
    );
} else {
    $encoded_string = $input_string;
}
echo $encoded_string;
?>


Output

Bonjour tout le monde! Ça va bien ?

Program 2: The following program demonstrates the mb_encode_numericentity() function.

PHP




<?php
$input_string = "Hello, こんにちは!";
  
// Encode characters from decimal 128 to 65535
$convmap = [0x80, 0xffff, 0, 0xffff];
$encoded_string
      mb_encode_numericentity($input_string, $convmap, "UTF-8");
echo $encoded_string;
?>


Output:

Hello, こんにちは!

Reference: https://www.php.net/manual/en/function.mb-encode-numericentity.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS