Core\Str::graphemes
Divides a string into its characters, the way a person counts them.
Core\Str::graphemes returns an array of strings, one for each character, in their original order. "héllo" gives "h", "é", "l", "l" and "o". An empty string gives an empty array.
One character on the screen can be several code points in the string. An accent can be a separate mark after its letter, a flag is two code points, and a family emoji can be seven. Each of these stays one piece. Unicode calls such a character a grapheme. Core\Str::length counts the same pieces, so the array always has Core\Str::length($s) elements.
To get the numbers of the code points instead, use Core\Str::codePoints.
The examples below divide a word into its letters, show that a flag and an accent stay whole, and reverse a name without breaking its accents.
Parameters
$sstringThe string to divide.
Return value
Returns array<string>. One string for each character, in order. An empty string gives [].
Examples
The letters of a word
<?nvs// Shows how to divide a word into its letters.
// `Core\Str::graphemes` returns one string for each letter. "é" is one letter.array<string> $letters = Core\Str::graphemes('Café');
// This prints `C-a-f-é` and then `4`.echo Core\Str::join($letters, '-'), "\n";echo Core\Arr::count($letters), "\n";
// An empty string has no letters, so this prints `0`.echo Core\Arr::count(Core\Str::graphemes('')), "\n";C-a-f-é40Flags and accents stay whole
<?nvs// Shows that a character made of several code points stays one piece.
// The first "é" is one code point. The second is an "e" and a separate accent mark.// The flag is two code points, and the thumb is a thumb and a skin tone.array<string> $words = ['é', "e\u{0301}", '🇩🇪', '👍🏽'];
// Each line prints the number of pieces and then the number of code points.// This prints `1 1`, `1 2`, `1 2` and `1 2`.foreach ($words as string $word) { echo Core\Arr::count(Core\Str::graphemes($word)), ' '; echo Core\Arr::count(Core\Str::codePoints($word)), "\n";}1 11 21 21 2Find characters a username may not use
<?nvs// Shows how to list the characters in a username that are not allowed, for an error message on a sign-up form.
class Username{ // A username may use only these characters. public static function allowed(): string { return 'abcdefghijklmnopqrstuvwxyz0123456789_'; }
// This loop checks each character once and keeps the ones that are not allowed. public static function invalid(string $name): array<string> { array<string> $found = []; foreach (Core\Str::graphemes($name) as string $character) { if (!Core\Str::contains(Username::allowed(), $character)) { $found[] = $character; } } return Core\Arr::unique($found); }}
// This prints `anna_m: ok`, then `anna.müller: . ü` and `zoë👋: ë 👋`.array<string> $names = ['anna_m', 'anna.müller', 'zoë👋'];foreach ($names as string $name) { array<string> $invalid = Username::invalid($name); if (Core\Arr::count($invalid) == 0) { echo $name, ": ok\n"; } else { echo $name, ': ', Core\Str::join($invalid, ' '), "\n"; }}anna_m: okanna.müller: . üzoë👋: ë 👋