Categories
PHP

How to change the string case?

In this tutorial, you’ll learn how to change the case of a string. For example, capitalize the first character of a string or word or change the case of the whole string to upper or lower case. This tutorial also explains how to alter the case of Multibyte characters.

Altering the case of a string is very easy with PHP’s four built-in functions:

  1. strtoupper()
  2. strtolower()
  3. ucfirst() and
  4. ucwords().

The above functions do not work on multibyte strings you should use the multibyte equivalents of those functions:

  1. mb_strtolower()
  2. mb_strtoupper()
  3. mb_convert_case().
  4. mb_ucfirst()

strtoupper

//Syntax
strtoupper(string $string): string

The strtoupper function converts string characters into upper case.

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

strtolower

//Syntax
strtolower(string $string): string

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

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

ucfirst

<?php
//Syntax
ucfirst(string $string): string

The ucfirst function capitalizes the first character of a string.

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

ucwords

<?php
//Syntax
ucwords(string $string, string $separators = " \t\r\n\f\v"): string

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

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

The optional $separators contains the word separator characters. See the following example:

<?php
 //Single separator |
 echo ucwords ('capitalizes|the|first|character of every word', '|');
 //Capitalizes|The|First|Character of every word

 //Multiple separator | and space
 echo ucwords ('capitalizes|the|first|character of every word', '| ');
 //Capitalizes|The|First|Character Of Every Word

Change Multibyte String Case

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 accept 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.

<?php
  mb_internal_encoding('UTF-8');

By setting up the internal encoding, it is not necessary to provide the encoding parameter for the multibyte functions.

mb_strtolower

<?php
//Syntax:
mb_strtolower(string $string, ?string $encoding = null): string

The mb_strtolower function lower cases all the characters in a multibyte string. If the second parameter $encoding is omitted, the internal character encoding value will be used.

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

 // Setting character encoding
 echo mb_strtolower($string,'UTF-8');
 //Prints: ἇ ђ б

mb_strtoupper

The mb_strtoupper function upper cases all the characters in a multi-byte string.

<?php
 $string = 'ἇ ђ б';
 echo mb_strtoupper($string);

 //Set character encoding
 echo mb_strtoupper($string,'UTF-8');
 //Prints: Ἇ Ђ Б

mb_convert_case

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 (Ä).

<?php
 $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

<?php
 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;
}

Manipulating Strings:

  1. Changing string case
  2. Comparing Strings
  3. Comparing Strings with Natural Order Algorithm
  4. Converting Strings into HTML
  5. Converting HTML Entities and Special Characters
  6. Formatting Strings
  7. Padding Strings
  8. Replacing Newlines with BR Tag and Trimming Whitespaces
  9. Converting character to code and code to character
  10. Repeat and Shuffle a String
  11. Count Characters and Words
  12. Manipulating substrings