大网数据广告活动
PHP 语言与运行手册

mb_encode_numericentity · Encode character to HTML numeric string reference

· 52CE 我爱测

mb_encode_numericentity — Encode character to HTML numeric string reference

mb_encode_numericentity

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

mb_encode_numericentity — Encode character to HTML numeric string reference

说明

function mb_encode_numericentity(
    string $string,
    array $map,
    ?string $encoding = null,
    bool $hex = false
): string

Converts specified character codes in string string from character code to HTML numeric character reference.

参数

string

The string being encoded.

map

map is array specifies code area to convert.

encoding

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

hex

Whether the returned entity reference should be in hexadecimal notation (otherwise it is in decimal notation).

返回值

The converted string.

错误/异常

Throws a ValueError if map contains any value that is not an int, float, bool, null, or a numeric string.

从 PHP 8.0.0 起,如果 encoding 是无效编码, 则会抛出 ValueError。 在 PHP 8.0.0 之前,会发出 E_WARNING。

更新日志

版本 说明
8.4.0 The mb_encode_numericentity() function now throws a ValueError if map contains any value that cannot be implicitly converted to int.
8.0.0 现在 encoding 可以为 null。
8.0.0 现在,如果 encoding 是无效编码, 会抛出 ValueError。 以前会发出 E_WARNING 并返回 false。

示例

示例 #1 map example

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

示例 #2 mb_encode_numericentity() example

<?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;"

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。