Core\Str::codePoints
Lists the numbers that Unicode gives to each symbol in a string.
Every symbol in Unicode has a number, called its code point. "A" is 65, "é" is 233 and "€" is 8364. Core\Str::codePoints returns these numbers as an array of uint, in order. An empty string gives an empty array.
A code point is not always a whole character. An accent can be its own code point, placed after a letter, and a flag is two code points. So "é" can give one number or two, depending on how it was written. To count or divide text the way a person sees it, use Core\Str::length or Core\Str::graphemes.
Core\Str::fromCodePoints does the opposite and builds a string from the numbers.
The examples below list the numbers of a short word, show one accented letter written in two ways, and find an invisible character in a pasted user name.
Parameters
$sstringThe string to read.
Return value
Returns array<uint>. One uint for each code point, in order. Each number is between 0 and 0x10FFFF and is never a surrogate. The result is [] for the empty string.
Examples
Numbers of a word
<?nvs// Shows how to list the Unicode number of each symbol in a string.
// This prints `72`, `105`, `32` and `8364`, one on each line.// 32 is the space and 8364 is the euro sign.foreach (Core\Str::codePoints('Hi €') as uint $point) { echo $point, "\n";}72105328364One letter two ways
<?nvs// Shows that one letter a person sees can be one code point or two.
// The first "é" is one code point. The second is "e" followed by an accent code point.string $together = "\u{e9}";string $separate = "e\u{301}";
// Both look the same and both are one character long. This prints `1` twice.echo Core\Str::length($together), "\n";echo Core\Str::length($separate), "\n";
// Their code points are different. This prints `233` and then `101 769`.echo Core\Str::join(Core\Arr::map(Core\Str::codePoints($together), fn(uint $p): string => "{$p}"), ' '), "\n";echo Core\Str::join(Core\Arr::map(Core\Str::codePoints($separate), fn(uint $p): string => "{$p}"), ' '), "\n";11233101 769Find an invisible character
<?nvs// Shows how to find an invisible character in a user name that somebody pasted into a form.
// The second name has a zero-width space (code point 8203) after "ad". You cannot see it.array<string> $names = ['admin', "ad\u{200B}min"];
// This prints `admin: ok` and then `admin: invisible character at position 2`.foreach ($names as string $name) { string $result = 'ok'; uint $position = 0; foreach (Core\Str::codePoints($name) as uint $point) { if ($point == 8203) { $result = "invisible character at position {$position}"; } $position = $position + 1; } echo $name, ': ', $result, "\n";}admin: okadmin: invisible character at position 2