PHP

How to change string case?

In this tutorial we’ll learn how to capitalize the string first character, capitalize all string words and alter the case of whole string to upper or lower. This tutorial also describes how to alter the case of multi-byte characters (string).

Altering the case of a string is very easy with PHP’s four built-in functions: strtolower, strtoupper, ucfirst and ucwords.

The strtoupper function converts string characters into upper case.

$string = 'A Sample STRING.';
echo strtoupper($string);
//prints: A SAMPLE STRING.

The strtolower function lower cases all the characters in a string.

$string = 'A Sample STRING.';
echo strtolower($string);
//prints: a sample string.

The ucfirst function capitalizes the first character of a string.

$string = 'a Sample STRING.';
echo ucfirst($string);
//prints: A Sample STRING.

The ucwords function capitalizes the first character of every word in a string.

$string = 'a sample string.';
echo ucwords($string);
//Prints: A Sample String.

Change multi-byte string case (UTF8)

The native string functions in PHP assume strings are an array of single bytes, so functions strtolower, strtoupper, ucwords and ucfirst will not work on multibyte strings, such as Japanese and Chinese characters. You should use the multibyte equivalents of those functions, such as mb_strtolower, mb_strtoupper and mb_convert_case.

The multi-byte functions accepts another optional parameter for the character encoding. If it is omitted, the internal character encoding value will be used.

We can set the internal character encoding by using mb_internal_encoding.

 mb_internal_encoding('UTF-8');

By setting up the internal encoding, we’ll not need to provide encoding parameter for the multi byte functions.

The mb_strtolower function lower cases all the characters in a multi-byte string.

$string = 'Ἇ Ђ Б';
echo mb_strtolower($string);

/* OR with character encoding*/
echo mb_strtolower($string,'UTF-8');

//Prints: ἇ ђ б

The mb_strtolower function lower cases all the characters in a multi-byte string.

$string = 'ἇ ђ б';
echo mb_strtoupper($string);
/*OR with character encoding */

echo mb_strtoupper($string,'UTF-8');

//Prints: Ἇ Ђ Б

The mb_convert_case function convert case on the basis of the Unicode character properties, it can convert any characters that have ‘alphabetic’ property, such as A-umlaut (Ä).

$string = 'Τάχιστη αλώπηξ βαφής ψημένη γη';
echo mb_convert_case($string, MB_CASE_UPPER);
//ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ

//You also can provide your desired encoding
echo mb_convert_case($string, MB_CASE_LOWER, 'UTF-8');
//τάχιστη αλώπηξ βαφής ψημένη γη

echo mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
//Τάχιστη Αλώπηξ Βαφής Ψημένη Γη

Howto capitalize the first character of a multi-byte string

PHP does not have a mb_ucfirst function but you can use the following custom function as an alternative.

mb_ucfirst function example

function mb_ucfirst($string, $encoding = null) {
 if (empty($string)) return $string;
 $encoding = is_null($encoding) ? mb_internal_encoding() : $encoding;
 $firstChr = mb_strtoupper(mb_substr($string, 0, 1,$encoding),$encoding);
 return $firstChr . mb_substr($string, 1, null, $encoding);
}

Creating mb_ucfirst function step by step

This step by step tutorial will elaborate the above mb_ucfirst function.

Step 1. Create function mb_ucfirst which will accept two parameters, first for the input string and the second for the character encoding. Assign NULL to encoding parameter as default values.

function mb_ucfirst($string, $encoding = null) {

}

Step 2. Check for empty string by validating it with empty function. empty function returns false if string value does not exist or equals to FALSE.

function mb_ucfirst($string, $encoding = null) {
 if (empty($string)) return;
}

Step 3. Validate second parameter $encoding with is_null function. is_null function returns true if value is null. If character encoding not provided we’ll assign internal encoding to $encoding parameter using mb_internal_encoding function.

function mb_ucfirst($string, $encoding = null) {
 if (empty($string)) return $string;
 if (is_null($encoding)) {
  $encoding = mb_internal_encoding();
 }
}

Step 4. Extract first character of the string using mb_substr function (similar to substr function but multi byte safe). mb_substr accepts four parameters : $string, $start, $length, $encoding. The $start value 0 and $lenght value 1 return the first character from the string.

function mb_ucfirst($string, $encoding = null) {
 if (empty($string)) return $string;
 if (is_null($encoding)) {
  $encoding = mb_internal_encoding();
 }
 $firstChr = mb_substr($string, 0, 1,$encoding);
}

Step 4. Change first character $firstChr case using mb_strtoupper function.

function mb_ucfirst($string, $encoding = null) {
 if (empty($string)) return $string;
 if (is_null($encoding)) {
  $encoding = mb_internal_encoding();
 }
 $firstChr = mb_substr($string, 0, 1,$encoding);
 $firstChr = mb_strtoupper($firstChr,$encoding);
}

Step 5. Now extract the remaining string (without the first character) from the original string using mb_substr function.

We’ll use 1 and null for start and length values respectively in mb_substr function, to extract all the remaining characters.

Note: If NULL is passed in length value the mb_substr function will extract all characters to the end of the string.

function mb_ucfirst($string, $encoding = null) {
 if (empty($string)) return $string;
 if (is_null($encoding)) {
  $encoding = mb_internal_encoding();
 }
 $firstChr = mb_substr($string, 0, 1,$encoding);
 $firstChr = mb_strtoupper($firstChr,$encoding);
 $remainingString = mb_substr($string, 1, null, $encoding);
}

Step 6. Now concate $firstChar and $remainingString with . and return it.

function mb_ucfirst($string, $encoding = null) {
 if (empty($string)) return $string;
 if (is_null($encoding)) {
  $encoding = mb_internal_encoding();
 }
 $firstChr = mb_substr($string, 0, 1,$encoding);
 $firstChr = mb_strtoupper($firstChr,$encoding);
 $remainingString = mb_substr($string, 1, null, $encoding);
 return $firstChr . $remainingString;
}
by BrainBellupdated
Advertisement: