Skip to content

Core\Str::fromCodePoints

Core\Str::fromCodePoints(array<uint> $codePoints): stringCore\Str::fromCodePoints(
    array<uint> $codePoints,
): string

Builds a string from a list of Unicode numbers.

Every symbol in Unicode has a number, called its code point. Core\Str::fromCodePoints takes an array of these numbers and returns one string that contains the symbols in the same order. [72, 105] gives "Hi", and an empty array gives "".

It does the opposite of Core\Str::codePoints. If you pass a string to Core\Str::codePoints and the result to Core\Str::fromCodePoints, you get the same string back.

Every number must be a code point. If one number is larger than 0x10FFFF or in the reserved range 0xD800 to 0xDFFF, the method throws a RuntimeError. It does not return the part of the string that was valid.

The examples below build a word from its numbers, catch the error for a list with one bad number, and remove invisible characters from a pasted user name.

Parameters

  • $codePointsarray<uint>The numbers of the symbols, in order. Each one is at most 0x10FFFF and is not in the surrogate range 0xD800 to 0xDFFF.

Return value

Returns string. The string. An empty array gives "".

Errors

  • RuntimeErrorAn element is above 0x10FFFF, or it is in the surrogate range 0xD800 to 0xDFFF. No part of the string is returned.

Examples

A word from numbers

<?nvs
// Shows how to build a string from a list of Unicode numbers, and how to get the numbers back.
// This prints `Grüße`.
array<uint> $numbers = [71, 114, 252, 223, 101];
string $word = Core\Str::fromCodePoints($numbers);
echo $word, "\n";
// `Core\Str::codePoints` returns the same numbers. This prints `true`.
echo Core\Str::codePoints($word) == $numbers ? 'true' : 'false', "\n";
Output
Grüße
true

One bad number

<?nvs
// Shows what happens when one number in the list is not a code point.
// 0xD800 is in the reserved range, so it is not a code point.
array<uint> $numbers = [79, 75, 0xD800];
// `Core\Str::fromCodePoints` throws a `RuntimeError`. No part of the string is returned,
// so `$text` keeps its first value. This prints `error` and then `(none)`.
string $text = '(none)';
try {
$text = Core\Str::fromCodePoints($numbers);
} catch (RuntimeError $error) {
echo "error\n";
}
echo $text, "\n";
Output
error
(none)

Remove invisible characters

<?nvs
// Shows how to remove invisible characters from a user name that somebody pasted into a form.
// 8203 is a zero-width space and 65279 is a byte order mark. You cannot see either of them.
array<uint> $invisible = [8203, 65279];
// The pasted name has both. It looks like "admin" but it is 7 code points long.
string $pasted = "\u{FEFF}ad\u{200B}min";
// This keeps every code point that is not invisible, and builds the name again.
array<uint> $kept = [];
foreach (Core\Str::codePoints($pasted) as uint $point) {
if (!Core\Arr::contains($invisible, $point)) {
$kept[] = $point;
}
}
string $clean = Core\Str::fromCodePoints($kept);
// This prints `admin` and then `true`.
echo $clean, "\n";
echo $clean == 'admin' ? 'true' : 'false', "\n";
Output
admin
true