Skip to content

Core\Str::lower

Changes every letter of a text to lower case.

Core\Str::lower takes one string and returns a copy where every upper-case letter is in lower case. It uses the rules of Unicode, so it works for letters with accents and for other alphabets too: "ÄRGER" gives "ärger", and the Greek "ΟΔΟΣ" gives "οδος". Digits, spaces and signs do not change.

The result can have a different number of bytes than the text you give it. A few letters change into two symbols when they become lower case.

Core\Str::upper does the opposite. Core\Str::lowerFirst changes only the first letter. To check if two texts are equal while ignoring case, use Core\Str::fold.

Good to know: there is no version of this method that works on single bytes.

Parameters

  • $sstringThe string to change.

Return value

Returns string. The string in lower case. Characters that are not letters do not change. The result can have a different length from $s.

Examples

A word in lower case

<?nvs
// Shows how to change a text to lower case.
// This prints `hello, world!`. The comma, the space and the `!` do not change.
echo Core\Str::lower('Hello, WORLD!'), "\n";
// Letters with accents change too. This prints `ärger über öl`.
echo Core\Str::lower('ÄRGER ÜBER ÖL'), "\n";
// Digits have no case. This prints `room 42b`.
echo Core\Str::lower('ROOM 42B'), "\n";
Output
hello, world!
ärger über öl
room 42b

Other alphabets

<?nvs
// Shows that `Core\Str::lower` works for every alphabet, not only for English letters.
array<string> $words = ['ÉCOLE', 'МОСКВА', 'ΟΔΟΣ'];
// This prints `école`, `москва` and `οδος`.
// In Greek, `Σ` at the end of a word becomes `ς`. Anywhere else it becomes `σ`.
foreach ($words as string $word) {
echo Core\Str::lower($word), "\n";
}
Output
école
москва
οδος

Check a file extension

<?nvs
// Shows how to check the extension of an uploaded file name, whatever case the user typed it in.
array<string> $uploads = ['holiday.jpg', 'SCAN.JPG', 'Report.Pdf', 'notes.txt'];
array<string> $allowed = ['.jpg', '.png', '.pdf'];
// `Core\Str::lower` changes the name to lower case first.
// Then one list of lower-case extensions is enough.
foreach ($uploads as string $name) {
string $lowered = Core\Str::lower($name);
bool $ok = false;
foreach ($allowed as string $extension) {
if (Core\Str::endsWith($lowered, $extension)) {
$ok = true;
}
}
// This prints `accepted` for the first three names and `rejected` for `notes.txt`.
echo $name, ': ', $ok ? 'accepted' : 'rejected', "\n";
}
Output
holiday.jpg: accepted
SCAN.JPG: accepted
Report.Pdf: accepted
notes.txt: rejected