mb_decode_numericentity

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

mb_decode_numericentityDecode HTML numeric string reference to character

Descrierea

mb_decode_numericentity ( string $str , array $convmap [, string $encoding = mb_internal_encoding() [, bool $is_hex = FALSE ]] ) : string

Convert numeric string reference of string str in a specified block to character.

Parametri

str

The string being decoded.

convmap

convmap is an array that specifies the code area to convert.

encoding

Parametrul encoding reprezintă codificarea caracterelor. Dacă el este omis, va fi utilizată valoarea internă a codificării caracterelor.

Valorile întoarse

The converted string.

Exemple

Example #1 convmap example

<?php
$convmap 
= array (
   
int start_code1int end_code1int offset1int mask1,
   
int start_code2int end_code2int offset2int mask2,
   ........
   
int start_codeNint end_codeNint offsetNint maskN );
// Specify Unicode value for start_codeN and end_codeN
// Add offsetN to value and take bit-wise 'AND' with maskN, 
// then convert value to numeric string reference.
?>

Example #2 convmap example escapes JavaScript string

<?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
          
];
  
// Char encoding is 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;
}
 
// Test data
$convmap = [ 0x00xffff00xffff ];
$msg '';
for (
$i=0$i 1000$i++) {
  
// chr() cannot generate correct UTF-8 data larger value than 128, use mb_decode_numericentity().
  
$msg .= mb_decode_numericentity('&#'.$i.';'$convmap'UTF-8');
}
 
// var_dump($msg);
var_dump(escape_javascript_string($msg));

A se vedea și