mb_strtoupper
mb_strtoupper($string, $encoding)
Converts all letters in a text to uppercase, accented letters included. The strtoupper() function handles only the letters a–z.
Parameters
- $string – the text to convert.
- $encoding – optional, the character encoding. The default is the value of mb_internal_encoding(), usually UTF-8.
Examples
echo mb_strtoupper("crème brûlée"); // CRÈME BRÛLÉE
// strtoupper does not convert accented letters
var_dump(strtoupper("été") === "ÉTÉ"); // bool(false)
var_dump(mb_strtoupper("été") === "ÉTÉ"); // bool(true)
// an uppercase first letter of text with accents
echo mb_strtoupper(mb_substr("élan", 0, 1)) . mb_substr("élan", 1); // Élan
// digits and other characters do not change
echo mb_strtoupper("café no. 5"); // CAFÉ NO. 5
// the German sharp s becomes two letters
echo mb_strtoupper("straße"); // STRASSE
echo mb_strlen("straße") . " and " . mb_strlen(mb_strtoupper("straße")); // 6 and 7
// conversion breaks HTML entities
echo mb_strtoupper("café"); // CAFÉ
Things to watch out for
- For text with accents always use mb_strtoupper. The strtoupper() function does not convert accented letters and in PHP 7 it can damage UTF-8 text depending on the locale setting.
- The length of the text can change. Since PHP 7.3 the German “ß” becomes “SS”, so do not rely on a fixed length of the result.
- Do not convert text that already contains HTML tags or entities. é becomes an invalid entity. Convert the original text and only then escape it with htmlspecialchars().
- When it is only about the look (a heading in capitals), keep the text in its original form and use text-transform: uppercase in CSS.
- The function for an uppercase first letter (mb_ucfirst) exists only since PHP 8.4, in older versions the code from the example replaces it.
Related functions:
strtoupper()
mb_strtolower()
mb_convert_case()