phpday 2027 - Call for Papers

mb_chr

(PHP 7 >= 7.2.0, PHP 8)

mb_chr — Return character by Unicode code point value

Beschreibung

function mb_chr(int $codepoint, ?string $encoding = null): string|false

Returns a string containing the character specified by the Unicode code point value, encoded in the specified encoding.

This function complements mb_ord().

Parameter-Liste

codepoint

A Unicode codepoint value, e.g. 128024 for U+1F418 ELEPHANT

encoding

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

Rückgabewerte

A string containing the requested character, if it can be represented in the specified encoding Bei einem Fehler wird false zurückgegeben..

Fehler/Exceptions

As of PHP 8.0.0, a ValueError is thrown if encoding is an invalid encoding. Prior to PHP 8.0.0, an E_WARNING was emitted instead.

Changelog

Version Beschreibung
8.0.0 encoding ist nun nullable (akzeptiert den null-Wert).
8.0.0 Now throws a ValueError if encoding is an invalid encoding. Previously, an E_WARNING was emitted and false was returned.

Beispiele

Beispiel #1 Testen unterschiedlicher Codepoints

<?php
$values = [65, 63, 0x20AC, 128024];
foreach ($values as $value) {
    var_dump(mb_chr($value, 'UTF-8'));
    var_dump(mb_chr($value, 'ISO-8859-1'));
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

string(1) "A"
string(1) "A"
string(1) "?"
string(1) "?"
string(3) "€"
bool(false)
string(4) "🐘"
bool(false)

Siehe auch

+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top