PHP – Case folding in a string using mb_convert_case()


mb_convert_case() is an inbuilt function in PHP that is used to perform case folding on a given string.

Syntax

string mb_convert_case(str $string, int $mode, str $encoding)

Parameters

mb_convert_case() accepts three parameters: $string, $mode and $encoding to perform case folding on a string.

  • $string− This parameter is used to return the string being converted.

  • $mode: The mode parameter is used for the mode of the conversion. It can be used for multibyte string conversion for MB_CASE_UPPER, MB_CASE_LOWER, MB_CASE_TITLE, MB_CASE_FOLD, MB_CASE_UPPER_SIMPLE, MB_CASE_LOWER_SIMPLE, MB_CASE_TITLE_SIMPLE, MB_CASE_FOLD_SIMPLE.

  • $encoding: This parameter is the character encoding. If it is omitted or null, then the internal character encoding value will be used

Return Values

mb_convert_case() is used to return the string mode of the conversion.

Note: From PHP 7.3.0, some multibyte functions are added as mode, such as MB_CASE_FOLD, MB_CASE_UPPER_SIMPLE, MB_CASE_LOWER_SIMPLE, MB_CASE_TITLE_SIMPLE, and MB_CASE_FOLD_SIMPLE.

Example 1

 Live Demo

<?php
   $string = "Hello World!, Welcome to the online Tutorial";

   // convert above string in upper case
   $string = mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
   echo $string;


   // It will convert given string in lower case
   $string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
   echo $string;
?>

Output

HELLO WORLD!, WELCOME TO THE ONLINE TUTORIALhello world!, welcome to the online tutorial

Example 2

 Live Demo

<?php
   $string = "Hello World!, Welcome to the online Tutorial";

   // MB_CASE_TITLE is used
   $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
   echo $string;

   // MB_CASE_UPPER_SIMPLE convert string in upper case
   $string = mb_convert_case($string, MB_CASE_UPPER_SIMPLE, "UTF-8");
   echo $string;
?>

Output

Hello World!, Welcome To The Online TutorialHELLO WORLD!, WELCOME TO THE ONLINE TUTORIAL

Updated on: 23-Aug-2021

303 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements