In PHP, converting string case is a common task. For handling multibyte character sets (such as Chinese, Japanese, Korean, etc.), PHP provides the mb_convert_case function for case conversion. Unlike the traditional strtoupper() and strtolower() functions, mb_convert_case supports multibyte encodings, making it more effective for strings containing non-Latin characters.
This article provides a detailed explanation of how to use the mb_convert_case function to convert strings into uppercase letters and its usage.
The mb_convert_case function is used to convert the case of multibyte strings. Its syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">mb_convert_case</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str</span></span>, </span><span><span class="hljs-keyword">int</span></span> </span><span><span class="hljs-variable">$mode</span></span>, </span><span><span class="hljs-keyword">string</span></span>|</span><span><span class="hljs-literal">null</span></span> </span><span><span class="hljs-variable">$encoding</span></span> = </span><span><span class="hljs-literal">null</span></span>): </span><span><span class="hljs-keyword">string</span></span></span>
$str: The string to be converted.
$mode: Conversion mode. It can be one of the following:
MB_CASE_UPPER: Converts all letters to uppercase.
MB_CASE_LOWER: Converts all letters to lowercase.
MB_CASE_TITLE: Converts the first letter of each word to uppercase (title case).
$encoding: Optional parameter that specifies the character encoding. If omitted, it defaults to the internal character encoding (usually UTF-8).
To convert all letters in a string to uppercase, you can use the MB_CASE_UPPER mode. Suppose we have a string and want to convert it entirely to uppercase.
<span><span><span class="hljs-meta"><?php</span></span>
<span><span class="hljs-comment">// Example string</span></span>
<span><span class="hljs-variable">$str</span> = <span class="hljs-string">"Hello, 你好,PHP"</span>;
<p>// Convert to uppercase using mb_convert_case<br>
$upper_str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");</p>
<p><span class="hljs-keyword">echo $upper_str;<br>
?>
Output:
<span>HELLO, 你好,PHP</span>
As seen in the output, the English portion of the string has been successfully converted to uppercase. The Chinese characters remain unchanged because they do not have case in multibyte encodings.
Convert all letters in a string to lowercase
<span><span class="hljs-meta"><?php</span>
<span>$str = <span class="hljs-string">"Hello, 你好,PHP"</span>;
<span>$lower_str = mb_convert_case($str, MB_CASE_LOWER, <span class="hljs-string">"UTF-8"</span>);
<span>echo $lower_str;
<span class="hljs-meta">?></span>
Output:
<span>hello, 你好,php</span>
Convert a string to title case (capitalize the first letter of each word)
<span><span class="hljs-meta"><?php</span>
<span>$str = <span class="hljs-string">"hello world, 你好,php"</span>;
<span>$title_str = mb_convert_case($str, MB_CASE_TITLE, <span class="hljs-string">"UTF-8"</span>);
<span>echo $title_str;
<span class="hljs-meta">?></span>
Output:
<span>Hello World, 你好,Php</span>
The mb_convert_case function also supports specifying the character encoding. If not specified, it uses the current internal character encoding. Common encodings include UTF-8, GB2312, and BIG5. To ensure accuracy, especially when handling strings with non-Latin characters, it is best to explicitly specify the encoding.
<span><span class="hljs-meta"><?php</span>
<span>$str = <span class="hljs-string">"你好,world"</span>;
<span>$upper_str = mb_convert_case($str, MB_CASE_UPPER, <span class="hljs-string">"UTF-8"</span>);
<span>echo $upper_str;
<span class="hljs-meta">?></span>
Output:
<span>你好,WORLD</span>
The mb_convert_case function only affects alphabetic characters. Other symbols and numbers remain unchanged.
If the input string contains different encodings, be sure to pass the correct $encoding parameter to avoid garbled text.
In PHP, mb_convert_case is part of the mbstring extension. Ensure that your PHP environment has this extension enabled.
With the mb_convert_case function, PHP developers can easily convert the alphabetic portion of multibyte strings into uppercase, lowercase, or title case. Compared to traditional case conversion functions, mb_convert_case provides better support for multibyte characters, making it especially suitable for handling strings in Chinese, Japanese, and other non-Latin languages.
Mastering the use of mb_convert_case not only helps you better control string formatting but also enhances your ability to work with internationalized text.