PHP mb_encode_numericentity() Function
Last Updated :
11 Mar, 2024
The mb_encode_numercentity() function is an inbuilt function in PHP that is used to encode HTML entities with numerical values encoded using either decimal or hexadecimal representation.
Syntax:
string mb_encode_numericentity(
string $string,
array $map,
string $encoding,
bool $hex
)
Parameters: This function accepts four parameters that are described below.
- $string: The input string to encode.
- $map: An array that sets the range of Unicode characters to encode. The array is of the form array (start, end, offset, mask).
- $encoding: The character encoding of the input and output strings.
- $hex: An array that sets the range of Unicode characters to encode. The array is of the form array (start, end, offset, mask).
Return Values: The mb_encode_numericentity() function returns the encoded string, or “false” if an error occurs.
Examples of PHP mb_encode_numericentity() Function
Example 1: The following program demonstrates the mb_decode_numercentity() function.
PHP
<?php
$text = "I ♥ PHP!" ;
$encodedText = mb_encode_numericentity( $text ,
[0x80, 0xffff, 0, 0xffff], 'UTF-8' );
echo $encodedText ;
?>
|
Example 2: The following program demonstrates the mb_decode_numercentity() function.
PHP
<?php
$text = "Hello, <b>World</b>" ;
$tagsToEncode = [ '<b>' , '<strong>' , '<i>' , '<em>' ];
foreach ( $tagsToEncode as $tag ) {
if ( strpos ( $text , $tag ) !== false) {
$encodedText = mb_encode_numericentity( $text ,
[0x80, 0xffff, 0, 0xffff], 'UTF-8' );
break ;
} else {
$encodedText = $text ;
}
}
echo $encodedText ;
?>
|
Output
Hello, <b>World</b>
Example 3: The following program demonstrates the mb_decode_numercentity() function.
PHP
<?php
$userContent = "<p>This is a <strong>test</strong> article with some entities.</p>" ;
function decodeUserContent( $content ) {
return mb_decode_numericentity( $content , array (0x0, 0x10FFFF, 0, 'UTF-8' ));
}
echo decodeUserContent( $userContent );
?>
|
Output
<p>This is a <strong>test</strong> article with some entities.</p>
Reference: https://www.php.net/manual/en/function.mb-encode-numericentity.php
Share your thoughts in the comments
Please Login to comment...