Php/docs/function.mb-decode-numericentity

提供:Dev Guides
< Php
移動先:案内検索

mb_decode_numericentity

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

mb_decode_numericentityHTML 数値エンティティを文字にデコードする


説明

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

文字列 str において指定した文字領域にある数値エンティティを変換し、 変換後の文字列を返します。


パラメータ

str
デコードする文字列。
convmap
convmap は変換するコード領域を指定する配列です。
encoding
encoding パラメータには文字エンコーディングを指定します。省略した場合は、 内部文字エンコーディングを使用します。
is_hex
このパラメータは使われていません。


返り値

変換された文字列を返します。


例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 );// start_codeN および end_codeN に Unicode値を指定// 値にoffsetNを追加、マスクmaskNを指定してビット毎の'AND'をとり、// 数値エンティティに値を変換します。?>

例2 convmap を使って JavaScript の文字列をエスケープする例

<?phpfunction 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*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;} // テストデータ$convmap = [ 0x0, 0xffff, 0, 0xffff ];$msg = ;for ($i=0; $i < 1000; $i++) {  // chr() では 128 より大きい UTF-8 データを正しく生成できないので、mb_decode_numericentity() を使います  $msg .= mb_decode_numericentity('&#'.$i.';', $convmap, 'UTF-8');} // var_dump($msg);var_dump(escape_javascript_string($msg));

参考