Skip to content

Core\Str::fold

Changes a string so that text which differs only in upper and lower case becomes the same.

Core\Str::fold changes every letter to one fixed form. "HELLO", "Hello" and "hello" all give "hello". Some letters change to more than one letter: "ß" becomes "ss", so "Straße" and "STRASSE" give the same result.

The result is a key for comparing and looking up text. It is not meant to be shown to a person. To show text in lower case, use Core\Str::lower, which keeps "ß" as it is.

In plain words: it is like writing every name in one style on an index card, so the cards sort and match no matter how each name was typed.

Good to know: Core\Str::compare with { caseInsensitive: true } does not treat "ß" and "SS" as equal. Fold both strings first when that matters.

The examples below compare two words without case, show how folding differs from lower, and find a customer by email address however it was typed.

Parameters

  • $sstringThe string to fold.

Return value

Returns string. The folded string. It is a key for comparing, not text to show: ß becomes ss and fi becomes fi. fold("STRASSE") == fold("straße") is true.

Examples

Compare without case

<?nvs
// Shows how to check whether two words are the same when upper and lower case do not matter.
array<string> $answers = ['YES', 'Yes', 'yes', 'no'];
// `Core\Str::fold` gives the same result for "YES", "Yes" and "yes".
// This prints `true` three times and then `false`.
foreach ($answers as string $answer) {
bool $same = Core\Str::fold($answer) == Core\Str::fold('yes');
echo $answer, ': ', $same ? 'true' : 'false', "\n";
}
Output
YES: true
Yes: true
yes: true
no: false

Fold is not lower

<?nvs
// Shows how `Core\Str::fold` is different from `Core\Str::lower`.
// `lower` keeps "ß" as it is, because the word must still look right.
// `fold` changes "ß" to "ss". This prints `straße` and then `strasse`.
echo Core\Str::lower('Straße'), "\n";
echo Core\Str::fold('Straße'), "\n";
// The two ways to write the street name are equal only after folding.
// This prints `false` and then `true`.
echo Core\Str::lower('STRASSE') == Core\Str::lower('Straße') ? 'true' : 'false', "\n";
echo Core\Str::fold('STRASSE') == Core\Str::fold('Straße') ? 'true' : 'false', "\n";
Output
straße
strasse
false
true

Find a customer by email

<?nvs
// Shows how to find a customer by email address, however the address was typed.
array<string> $customers = ['Ada.Lovelace@Example.com', 'bob@example.com'];
// The array uses the folded address as its key. Each address is folded once, here.
array<string> $byEmail = [];
foreach ($customers as string $email) {
$byEmail[Core\Str::fold($email)] = $email;
}
// A login form sends the address in another case. Folding it finds the same key.
// This prints `found Ada.Lovelace@Example.com` and then `not found`.
array<string> $typed = ['ADA.LOVELACE@example.COM', 'eve@example.com'];
foreach ($typed as string $login) {
string $key = Core\Str::fold($login);
if (Core\Arr::hasKey($byEmail, $key)) {
echo 'found ', $byEmail[$key], "\n";
} else {
echo "not found\n";
}
}
Output
found Ada.Lovelace@Example.com
not found