Current Location: Home> Latest Articles> What is the Basic Usage of the mb_strtoupper Function? How to Quickly Master This Multibyte String Uppercase Conversion Function?

What is the Basic Usage of the mb_strtoupper Function? How to Quickly Master This Multibyte String Uppercase Conversion Function?

M66 2025-06-22

In PHP, when handling multibyte character sets such as UTF-8, GBK, Big5, etc., traditional string functions like strtoupper() may not correctly process these characters. In such cases, we can use the mb_strtoupper() function, which is provided by the multibyte string extension (mbstring) to convert strings to uppercase. Today, we will explore the basic usage of mb_strtoupper() and explain how to quickly master this function.

1. Overview of the mb_strtoupper() Function

The mb_strtoupper() function converts the alphabetic characters in a given string to uppercase. Unlike strtoupper(), mb_strtoupper() can properly handle multibyte character sets, ensuring that characters such as Chinese and Japanese are correctly processed under UTF-8 encoding.

Function Definition

mb_strtoupper(string $str, string $encoding = null): string
  • $str: The input string to be converted to uppercase.

  • $encoding: Optional parameter to specify the character encoding. The default value is NULL, which means the internal character encoding (usually UTF-8) will be used.

Notes

  • mb_strtoupper() is designed for handling multibyte encoded strings and is typically used with encodings like UTF-8, Shift-JIS, EUC-JP, etc.

  • If the mbstring extension is not installed, PHP will not support this function. Therefore, make sure the mbstring extension is installed and enabled before using it.

2. Basic Usage

The most common usage is to directly convert a string to uppercase. For example, converting a Chinese or English string to uppercase:

Example 1: Converting an English String to Uppercase

<?php
$str = "hello world";
echo mb_strtoupper($str);  // Outputs "HELLO WORLD"
?>
</span>

Example 2: Converting a Chinese String to Uppercase

<?php
$str = "你好,世界";
echo mb_strtoupper($str);  // Outputs "你好,世界"
?>
</span>

In the examples above, mb_strtoupper() correctly converts the English string to uppercase. For Chinese characters, since there is no case distinction, the string remains unchanged.

3. Specifying Encoding

mb_strtoupper() allows specifying the character encoding through the encoding parameter. By default, it uses PHP's internal character encoding (usually UTF-8), but sometimes we need to handle strings in other encodings, so we can explicitly specify the encoding.

Example 3: Specifying Encoding as GBK

<?php
$str = "你好,世界";
echo mb_strtoupper($str, "GBK");  // Outputs uppercase in GBK encoding
?>
</span>

Here, the encoding is specified as GBK. The function converts the string to uppercase according to the specified encoding. If the encoding is incorrect, it may cause errors or garbled output.

4. Handling Strings with Special Characters

The mb_strtoupper() function can not only handle normal characters but also correctly process strings containing special characters.

Example 4: String Containing Special Characters

<?php
$str = "Hello, 世界! 你好!";
echo mb_strtoupper($str);  // Outputs "HELLO, 世界! 你好!"
?>
</span>

In this example, the English part is converted to uppercase, while the Chinese characters remain unchanged. Special characters like commas and exclamation marks are also preserved.

5. Comparison Between mb_strtoupper() and strtoupper()

For single-byte character sets such as ISO-8859-1 or ASCII, strtoupper() is sufficient. However, if the string contains multibyte characters (e.g., Chinese or Japanese), strtoupper() will not work properly and may even result in garbled output.

Example 5: Incorrect Handling of Chinese Strings by strtoupper()

<?php
$str = "你好,世界";
echo strtoupper($str);  // Outputs error or garbled text
?>
</span>

The code above may produce garbled output because strtoupper() does not support multibyte characters, whereas mb_strtoupper() handles them correctly.

6. Conclusion

mb_strtoupper() is a very useful tool when working with multibyte character sets, especially for Chinese, Japanese, and similar scripts. In PHP, using this function ensures that multibyte characters are correctly converted to uppercase without causing garbled text or incorrect behavior. Mastering the basic usage of mb_strtoupper() can not only improve coding efficiency but also ensure that applications properly handle various character encodings, thereby enhancing the user experience.