Current Location: Home> Function Categories> mb_encode_numericentity

mb_encode_numericentity

Encode characters into HTML numeric string references
Name:mb_encode_numericentity
Category:Multi-byte string
Programming Language:php
One-line Description:Encode non-ASCII characters in a string into character entities that are decimal or hexadecimal

Function name: mb_encode_numericalentity()

Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7

Function description: Encode non-ASCII characters in a string into character entities in decimal or hexadecimal

usage:

 string mb_encode_numericentity ( string $str , array $convmap , string $encoding = mb_internal_encoding() , bool $is_hex = false ): string

parameter:

  • $str: A string that needs to be encoded.
  • $convmap: an array containing the mapping from characters to numeric values. The format of the array is array (character start, character end, cardinality, converted string encoding). For example, to convert all characters into hexadecimal entities, you can use array(0, 0x10FFFF, 16, '&#x%X;').
  • $encoding: Optional parameter, specify string encoding, default to the current internal encoding.
  • $is_hex: Optional parameter, specifying whether to encode the entity into hexadecimal. The default is false, which means decimal encoding is used.

Return value: encoded string.

Example:

 $str = "Hello, 世界!"; $convmap = array(0x80, 0x10FFFF, 0, 0xFFFD); $encoded_str = mb_encode_numericentity($str, $convmap, 'UTF-8', true); echo $encoded_str;

Output:

 Hello, 世界!

In the above example, we encode the non-ASCII characters in the string "Hello, World!" as a hexadecimal character entity. The hexadecimal code of the character "world" is 4E16, and the hexadecimal code of the character "world" is 754C, so the output result is "Hello, world!".

Similar Functions
Popular Articles