Core\Str::NormalForm
The four spellings Unicode allows for the same text, and the one Core\Str::normalize rewrites into.
Some characters can be written in more than one way. é can be a single character, or the letter e followed by a mark that adds the accent. The two look identical on screen and are not the same text, so a comparison says they differ and a search for one does not find the other.
Core\Str::NormalForm is declared in the class Core\Str. Core\Str::normalize takes it as its second argument.
In plain words: normalizing is agreeing on one spelling before you store text or compare it.
Nfc composes, so é becomes one character, and it is the form to store and compare in. Nfd goes the other way and keeps the accent separate. Both keep everything, so text survives a round trip between them.
Nfkc and Nfkd compose and decompose the same way, and also unify characters that merely look alike: fi becomes fi, ① becomes 1. That throws a distinction away, which is what you want for a search key and not what you want for text you are keeping.
Examples
The same word spelled two ways
<?nvs// The same word, written two ways. In the first, `é` is a single character. In// the second it is the letter `e` followed by a mark that adds the accent.// They look the same and they are not the same text.
string $typed = 'café';string $pasted = Core\Str::fromCodePoints([99, 97, 102, 101, 769]);
echo 'both read: ', $typed, ' and ', $pasted, "\n";echo 'characters: ', Core\Str::length($typed), ' and ', Core\Str::length($pasted), "\n";echo 'the same text: ', (Core\Str::compare($typed, $pasted) == 0) ? 'yes' : 'no', "\n";
// Putting both into the same form is what makes the comparison honest. `Nfc`// is the form to use for text you are storing and comparing.string $one = Core\Str::normalize($typed, Core\Str::NormalForm::Nfc);string $other = Core\Str::normalize($pasted, Core\Str::NormalForm::Nfc);echo 'the same text once normalized: ', (Core\Str::compare($one, $other) == 0) ? 'yes' : 'no', "\n";both read: café and cafécharacters: 4 and 4the same text: nothe same text once normalized: yesWhat each of the four forms does
<?nvs// This program converts one word to all four forms. The word has an accented// letter and `fi`. `fi` is one character that some typefaces use for `f` and `i`.
string $subject = 'fiancé';
array<string> $forms = ['as typed' => $subject];$forms['Nfc'] = Core\Str::normalize($subject, Core\Str::NormalForm::Nfc);$forms['Nfd'] = Core\Str::normalize($subject, Core\Str::NormalForm::Nfd);$forms['Nfkc'] = Core\Str::normalize($subject, Core\Str::NormalForm::Nfkc);$forms['Nfkd'] = Core\Str::normalize($subject, Core\Str::NormalForm::Nfkd);
foreach ($forms as string $name => string $text) { echo $name, ': ', $text, ' (', Core\Str::length($text), " characters)\n";}
// `Nfc` and `Nfd` store the accent in different ways, but both print 5 characters.// The `k` forms replace `fi` with `f` and `i`, so they print 6. This cannot be undone.as typed: fiancé (5 characters)Nfc: fiancé (5 characters)Nfd: fiancé (5 characters)Nfkc: fiancé (6 characters)Nfkd: fiancé (6 characters)Two people cannot take the same name
<?nvs// A sign-up form where no two people may have the same name. Comparing what people// typed is not enough. Two people can type the same name as two different strings.
class Names{ // `key` returns the name for comparing, not for showing. It lowercases the name, // and `Nfkc` writes accents and look-alike characters in one standard way. public static function key(string $name): string { return Core\Str::normalize(Core\Str::lower($name), Core\Str::NormalForm::Nfkc); }}
array<string> $taken = [];$taken[Names::key('Sofía')] = 'registered last week';
// Three people sign up. The first typed the accent as a separate character.// The second typed a wide capital S. The third is a different person.array<string> $asking = [ 'the accent typed apart' => Core\Str::fromCodePoints([83, 111, 102, 105, 769, 97]), 'a wide capital S' => 'Sofía', 'a different person' => 'Mira',];
foreach ($asking as string $who => string $name) { string $key = Names::key($name); echo $who, ': ', (($taken[$key] ?? '') == '') ? 'free' : 'taken', "\n"; $taken[$key] = $who;}the accent typed apart: takena wide capital S: takena different person: free