Skip to content

Core\Str::normalize

Changes a text into one fixed way of writing it, so that two texts which look the same are also equal.

Unicode often has two ways to write the same letter. é can be one character, or the letter e followed by an accent mark. Both look the same on the screen, but they are different strings, so == and Core\Str::contains treat them as different.

Core\Str::normalize takes a string and a form, and returns the string in that form. There are four forms in Core\Str::NormalForm. Nfc writes é as one character, and it is the form to use when you store and compare text. Nfd writes it as two. Nfkc and Nfkd also replace characters that only look like others: full-width 1 becomes 1, and fi becomes fi. That change cannot be undone.

A text of ASCII characters is returned unchanged in every form.

The examples show a search, full-width digits, and file names from different computers.

Parameters

  • $sstringThe string to normalize.
  • $formCore\Str::NormalFormWhich form to use. Nfc and Nfd keep every character's meaning. Nfkc and Nfkd also replace look-alike characters: fi becomes fi.

Return value

Returns string. The normalized string. Nfc is the form to store and compare text in. A string of ASCII characters is returned unchanged.

Examples

Find a word with an accent

<?nvs
// Shows how to search a text for a word with an accent. The text and the word
// may store the accent in two different ways.
// In this text, `é` is the letter `e` followed by a separate accent mark.
string $review = 'A great ' . Core\Str::fromCodePoints([99, 97, 102, 101, 769]) . ' near the station.';
string $search = 'café';
// This prints `false`. The two strings look the same, but their characters differ.
echo Core\Str::contains($review, $search) ? 'true' : 'false', "\n";
// `Nfc` changes both strings to the same form. This prints `true`.
string $text = Core\Str::normalize($review, Core\Str::NormalForm::Nfc);
string $word = Core\Str::normalize($search, Core\Str::NormalForm::Nfc);
echo Core\Str::contains($text, $word) ? 'true' : 'false', "\n";
Output
false
true

Full width digits

<?nvs
// Shows how `Nfkc` changes full-width digits, which some Asian keyboards type,
// into the digits `0` to `9`.
string $typed = '123';
// `as ?int` converts a value to a whole number. `123` is not written with
// the digits `0` to `9`, so the result is `null`, and this prints `0`.
echo ($typed as ?int) ?? 0, "\n";
// After `Nfkc`, the text is `123`. This prints `123` and then `124`.
string $digits = Core\Str::normalize($typed, Core\Str::NormalForm::Nfkc);
echo $digits, "\n";
echo (($digits as ?int) ?? 0) + 1, "\n";
Output
0
123
124

Match uploaded file names

<?nvs
// Shows how to find a file by its name when the names come from different
// computers. Some computers store `é` as one character, and some store it as
// `e` and an accent mark.
class FileNames
{
// Changes a name to `Nfc`, the form to store and compare text in.
public static function key(string $name): string
{
return Core\Str::normalize($name, Core\Str::NormalForm::Nfc);
}
}
// The names that users uploaded. The second one uses the separate accent mark.
array<string> $uploaded = [
'Invoice March.pdf',
'R' . Core\Str::fromCodePoints([101, 769]) . 'sum' . Core\Str::fromCodePoints([101, 769]) . '.pdf',
'Photo.jpg',
];
// This loop prints `match` for `Résumé.pdf` and `no match` for the other two.
string $wanted = FileNames::key('Résumé.pdf');
foreach ($uploaded as string $name) {
echo $name, ': ', FileNames::key($name) == $wanted ? 'match' : 'no match', "\n";
}
Output
Invoice March.pdf: no match
Résumé.pdf: match
Photo.jpg: no match