Skip to content

Core\Str::upper

Changes every letter of a text to upper case.

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

The result can be longer than the text you give it. A few letters change into two or three letters when they become upper case. For example, the German "ß" becomes "SS", so "straße" gives "STRASSE".

Core\Str::lower does the opposite. Core\Str::upperFirst 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 upper case. Characters that are not letters do not change. The result can be longer than $s.

Examples

A word in upper case

<?nvs
// Shows how to change a text to upper case.
// This prints `HELLO, WORLD!`. The comma, the space and the `!` do not change.
echo Core\Str::upper('Hello, world!'), "\n";
// `ß` becomes two letters, so the result is longer. This prints `STRASSE`.
echo Core\Str::upper('straße'), "\n";
// Digits have no case. This prints `ROOM 42B`.
echo Core\Str::upper('room 42b'), "\n";
Output
HELLO, WORLD!
STRASSE
ROOM 42B

Other alphabets

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

Check a voucher code

<?nvs
// Shows how to check a voucher code, whatever case the customer typed it in.
array<string> $typed = ['summer24', ' Summer24 ', 'SUMMER24', 'winter24'];
string $valid = 'SUMMER24';
// `Core\Str::trim` removes the spaces around the code.
// Then `Core\Str::upper` changes it to upper case, so one comparison is enough.
foreach ($typed as string $code) {
string $clean = Core\Str::upper(Core\Str::trim($code));
// This prints `accepted` for the first three codes and `rejected` for `winter24`.
echo '[', $code, ']: ', $clean == $valid ? 'accepted' : 'rejected', "\n";
}
Output
[summer24]: accepted
[ Summer24 ]: accepted
[SUMMER24]: accepted
[winter24]: rejected