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 – Case folding in a string using mb_convert_case()
The mb_convert_case() function is an inbuilt PHP function used to perform case folding on a given string. It supports multibyte character encodings and offers various conversion modes for different case transformations.
Syntax
string mb_convert_case(string $string, int $mode, ?string $encoding = null)
Parameters
The mb_convert_case() function accepts three parameters ?
$string − The input string to be converted.
-
$mode − The conversion mode. Available constants include:
-
MB_CASE_UPPER− Convert to uppercase -
MB_CASE_LOWER− Convert to lowercase -
MB_CASE_TITLE− Convert to title case -
MB_CASE_FOLD− Case folding (PHP 7.3+) -
MB_CASE_UPPER_SIMPLE− Simple uppercase (PHP 7.3+) -
MB_CASE_LOWER_SIMPLE− Simple lowercase (PHP 7.3+)
-
$encoding − Optional character encoding. If omitted, the internal character encoding is used.
Return Value
Returns the converted string according to the specified mode.
Example 1 - Basic Case Conversion
<?php
$string = "Hello World!, Welcome to the online Tutorial";
// Convert to uppercase
$upper = mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
echo "Uppercase: " . $upper . "<br>";
// Convert to lowercase
$lower = mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
echo "Lowercase: " . $lower . "<br>";
?>
Uppercase: HELLO WORLD!, WELCOME TO THE ONLINE TUTORIAL Lowercase: hello world!, welcome to the online tutorial
Example 2 - Title Case and Simple Conversions
<?php
$string = "hello world!, welcome to the online tutorial";
// Convert to title case
$title = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
echo "Title Case: " . $title . "<br>";
// Simple uppercase conversion (PHP 7.3+)
$upperSimple = mb_convert_case($string, MB_CASE_UPPER_SIMPLE, "UTF-8");
echo "Upper Simple: " . $upperSimple . "<br>";
?>
Title Case: Hello World!, Welcome To The Online Tutorial Upper Simple: HELLO WORLD!, WELCOME TO THE ONLINE TUTORIAL
Conclusion
The mb_convert_case() function provides robust case conversion for multibyte strings. Use appropriate mode constants based on your needs and always specify encoding for consistent results across different character sets.
