Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP – Make an upper case string using mb_strtoupper()
In PHP, mb_strtoupper() is a multibyte string function that converts all alphabetic characters in a string to uppercase. Unlike the regular strtoupper() function, it properly handles multibyte characters and various character encodings.
Syntax
string mb_strtoupper(string $string, ?string $encoding = null)
Parameters
mb_strtoupper() accepts two parameters: $string and $encoding.
$string− The string being converted to uppercase.
$encoding− The character encoding. If omitted or null, the internal character encoding value will be used.
Return Value
Returns a string with all alphabetic characters converted to uppercase.
Example
Here's how to use mb_strtoupper() to convert a string to uppercase −
<?php
$string = "Hello World!, Welcome to online tutorials";
$result = mb_strtoupper($string);
echo $result;
?>
HELLO WORLD!, WELCOME TO ONLINE TUTORIALS
Working with Different Encodings
The function can handle different character encodings properly −
<?php
// UTF-8 string with special characters
$string = "café résumé naïve";
$result = mb_strtoupper($string, 'UTF-8');
echo $result . "<br>";
// Without specifying encoding
$result2 = mb_strtoupper($string);
echo $result2;
?>
CAFÉ RÉSUMÉ NAÏVE CAFÉ RÉSUMÉ NAÏVE
Conclusion
The mb_strtoupper() function is essential for properly converting multibyte strings to uppercase, especially when working with international characters. Always use this function instead of strtoupper() when dealing with UTF-8 or other multibyte encodings.
