mb_encode_numericentity

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

説明

string mb_encode_numericentity(
    string $string,
    array $map,
    stringnull $encoding = null,
    bool $hex = false
)

stringの中で指定した文字コードを 文字コードから HTML 数値エンティティに変換します。

パラメータ

string

エンコードする文字列。

map

map は、変換するコード領域を指定する配列です。

encoding

encoding パラメータには文字エンコーディングを指定します。省略した場合、もしくは null の場合は、 内部文字エンコーディングを使用します。

hex

返されたエンティティのリファレンスが16進記法であるべきかどうか (false の場合、10進記法です)

戻り値

変換後の文字列を返します。

エラー / 例外

map が整数のリストでない場合、 ValueError がスローされます。

変更履歴

バージョン 説明
8.4.0 map が整数のリストでない場合、 mb_encode_numericentityValueError をスローするようになりました。
8.0.0 encoding は、nullable になりました。

例1 map の例

<?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
// it converts value to numeric string reference.
?>

例2 mb_encode_numericentity の例

<?php

$str = "aAæÆあア𩸽";

/* Convert all UTF8 characters up to 4 bytes to HTML numeric character reference */
$convmap = [0, 0x1FFFFF, 0, 0x10FFFF];
var_dump(mb_encode_numericentity($str, $convmap, "utf8"));

/* Converts only 2-byte and 4-byte UTF8 characters to HTML numeric character reference */
$convmap = [
    0x80, 0x7FF, 0, 0x10FFFF,
    0x10000, 0x1FFFFF, 0, 0x10FFFF,
];
var_dump(mb_encode_numericentity($str, $convmap, "utf8"));
?>

上の例の出力は以下となります。

string(46) "&#97;&#65;&#230;&#198;&#12354;&#12450;&#40509;"
string(28) "aA&#230;&#198;あア&#40509;"

参考

  • mb_decode_numericentity