Using mb_strtoupper() to Convert Strings to Uppercase in PHP
This article explains the PHP mb_strtoupper() function, detailing its syntax, parameters, return value, and demonstrates usage that converts English and Unicode strings to uppercase, highlighting optional encoding handling, including how to set internal encoding and handle multibyte characters correctly.
The mb_strtoupper() function in PHP converts all alphabetic characters in a string to uppercase, optionally using a specified character encoding.
Signature: string mb_strtoupper(string $str [, string $encoding = mb_internal_encoding()])
Description: The function returns the input string with all letters transformed to uppercase according to the given or internal encoding.
Parameters:
str : The string to be converted to uppercase.
encoding (optional): The character encoding to use; if omitted, the internal encoding is applied.
Return value: The uppercase version of $str .
Example 1 (English text):
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = mb_strtoupper($str);
echo $str;
// Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
?>Example 2 (Unicode text):
<?php
$str = "Τ?χιστη αλ?πηξ βαφ?? ψημ?νη γη, δρασκελ?ζει υπ?ρ νωθρο? κυν??";
$str = mb_strtoupper($str, 'UTF-8');
echo $str;
// Prints Τ?ΧΙΣΤΗ ΑΛ?ΠΗΞ ΒΑΦ?Σ ΨΗΜ?ΝΗ ΓΗ, ΔΡΑΣΚΕΛ?ΖΕΙ ΥΠ?Ρ ΝΩΘΡΟ? ΚΥΝ?Σ
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.