mb_encode_numericentity
PHP Manual
PHP Manual»Multibyte String Funktionen»mb_encode_numericentity

mb_encode_numericentity

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

mb_encode_numericentity — Encode character to HTML numeric string reference

Beschreibung

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.

Parameter-Liste

string

The string being encoded.

map

map is array specifies code area to convert.

encoding

Der Parameter encoding legt die Zeichenkodierung fest. Wird er nicht übergeben, so wird die interne Zeichenkodierung genutzt.

hex

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

Rückgabewerte

The converted string.

Fehler/Exceptions

Throws a ValueError if map contains any value that is not an Integer, Float, Boolean, null, or a numeric string.

Changelog

Version Beschreibung
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 ist nun nullable (akzeptiert den null-Wert).

Beispiele

Beispiel #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.
?>

Beispiel #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"));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch

To Top