Wednesday, July 3, 2024
HomeLanguagesPhpPHP 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! &#xC7;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!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments