mb_decode_numericentity

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

mb_decode_numericentityДекодирует ссылку на числовую строку HTML в символ

Описание

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

Преобразует строку чисел string (string) в заданном блоке в символ.

Список параметров

string

Строка (string) для декодирования.

map

map - массив (array), который задаёт диапазон кодов символов.

encoding

Параметр encoding представляет собой символьную кодировку. Если он опущен или равен null, вместо него будет использовано значение внутренней кодировки.

is_hex

Параметр не используется.

Возвращаемые значения

Преобразованная строка (string).

Список изменений

Версия Описание
8.0.0 Теперь параметр encoding может принимать значение null.

Примеры

Пример #1 Пример использования map

<?php
$convmap 
= array (
   
int start_code1int end_code1int offset1int mask1,
   
int start_code2int end_code2int offset2int mask2,
   ........
   
int start_codeNint end_codeNint offsetNint maskN );
// Задайте значения Юникода для start_codeN и end_codeN
// Добавьте к значению offsetN и сложите побитово с maskN,
// затем преобразуйте результат в число.
?>

Пример #2 Пример экранирования строки JavaScript с помощью map

<?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
          
];
  
// Кодировка символа 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*88);
      
$v base_convert($u1610);
      if (
$v 256 && $map[$v]) {
        
$encoded .= '\\x'.substr($u6,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;
}

// Данные для теста
$convmap = [ 0x00xffff00xffff ];
$msg '';
for (
$i=0$i 1000$i++) {
  
// chr() не может сгенерировать корректный символ UTF-8 больший, чем 128. Используем mb_decode_numericentity().
  
$msg .= mb_decode_numericentity('&#'.$i.';'$convmap'UTF-8');
}

// var_dump($msg);
var_dump(escape_javascript_string($msg));

Смотрите также

add a note add a note

User Contributed Notes 3 notes

up
1
dev at glossword info
20 years ago
Just two great functions for daily use:

/* Converts any HTML-entities into characters */
function my_numeric2character($t)
{
    $convmap = array(0x0, 0x2FFFF, 0, 0xFFFF);
    return mb_decode_numericentity($t, $convmap, 'UTF-8');
}
/* Converts any characters into HTML-entities */
function my_character2numeric($t)
{
    $convmap = array(0x0, 0x2FFFF, 0, 0xFFFF);
    return mb_encode_numericentity($t, $convmap, 'UTF-8');
}
print my_numeric2character('&#8217; &#7936; &#226;');
print my_character2numeric(' ');
up
0
donovan at conduit it
18 years ago
note that at this time it seems that mb_decode_numericentity() only works with decimal entities and not hexadecimal entities.  This fact would have saved me a good hour of time in debugging.

For those who need to convert hex entities try first converting them all to decimal entities with a combination of the preg_replace() and hexdec() functions.
up
-1
fernandosilveira at yahoo dot com dot br
3 years ago
Be careful!
In addition to translate numeric entities to chars on specified target encoding, this function encodes every character from input string to the specified target encodin, even if the characters are outside the range defined by the conversion map.
To Top