mb_decode_numericentity 根据 HTML 数字字符串解码成字符

发表日期:2021-07-01 08:55:51 | 来源: | | 浏览(781) 分类:多字节字符串 函数

mb_decode_numericentity

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

mb_decode_numericentity根据 HTML 数字字符串解码成字符

说明

mb_decode_numericentity(string $str, array $convmap, string $encoding = mb_internal_encoding()): string

将数字字符串的引用str 按指定的字符块转换成字符串。

参数


  • str

  • 要解码的 string

  • convmap

  • convmap 是一个 array,指定了要转换的代码区域。

  • encoding

  • encoding参数为字符编码。如果省略或是 null,则使用内部字符编码。


返回值

转换后的字符串

范例


示例 #1 convmap 例子

<?php 
$convmap = array (int start_code1, int end_code1, int offset1, int mask1,int start_code2, int end_code2, int offset2, int mask2,   ........   int start_codeN, int end_codeN, int offsetN, int 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.
?>


参见




示例 #2 convmap 的例子: 编码(escape) JavaScript 字符串

<?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, 
  490,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, 
  990,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, 
  1491,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,
  1991,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, 
  2491,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*8, 8);
      $v = base_convert($u, 16, 10);
      if ($v < 256 && $map[$v]) {
        $encoded .= '\\x'.substr($u, 6,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 = [ 0x0, 0xffff, 0, 0xffff ];
$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));


集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。