Converting Between Uppercase, Lowercase, and Title Case in PHP Strings

Converting Between Uppercase, Lowercase, and Title Case in PHP Strings

Learn how to convert strings to uppercase, lowercase, and title case in PHP using strtoupper(), strtolower(), and ucwords(). Handle multibyte characters correctly with mbstring.

Introduction

Converting text to different cases is a common requirement in data processing, user input handling, and formatting text dynamically. PHP provides several built-in functions for changing string case:

  • strtoupper() – Converts text to uppercase
  • strtolower() – Converts text to lowercase
  • ucwords() – Capitalizes the first letter of each word
  • ucfirst() – Capitalizes the first letter of a string
  • mbstring functions – Handles UTF-8 and multibyte characters correctly

This guide covers:

  • Converting strings to uppercase and lowercase
  • Handling title case (capitalized words)
  • Using mbstring functions for non-ASCII characters
  • Best practices for case conversion in PHP applications

1. Converting Strings to Uppercase with strtoupper()

The strtoupper() function converts all characters in a string to uppercase.

Example: Converting to Uppercase

$text = "hello world!";
$uppercase = strtoupper($text);

echo $uppercase; // Output: HELLO WORLD!
  • Works with ASCII characters but does not support multibyte (UTF-8) text.

Using mb_strtoupper() for UTF-8 Support

For multibyte characters, such as ä, é, ñ, ü, use mb_strtoupper().

$text = "café";
$uppercase = mb_strtoupper($text, "UTF-8");

echo $uppercase; // Output: CAFÉ
  • strtoupper("café") may not convert é correctly.
  • mb_strtoupper() ensures correct uppercase conversion for international characters.

2. Converting Strings to Lowercase with strtolower()

The strtolower() function converts all uppercase letters to lowercase.

Example: Converting to Lowercase

$text = "WELCOME TO PHP!";
$lowercase = strtolower($text);

echo $lowercase; // Output: welcome to php!

Using mb_strtolower() for UTF-8 Support

$text = "GÖTTINGEN";
$lowercase = mb_strtolower($text, "UTF-8");

echo $lowercase; // Output: göttingen
  • strtolower() may fail for non-ASCII characters like Ö and Ü.
  • mb_strtolower() ensures correct lowercase conversion.

3. Capitalizing Words Using ucwords()

ucwords() capitalizes the first letter of each word in a string.

Example: Title Case Conversion

$text = "hello world, welcome to php.";
$titleCase = ucwords($text);

echo $titleCase; // Output: Hello World, Welcome To Php.
  • Each word's first letter is capitalized, but articles, conjunctions, and prepositions are also capitalized incorrectly.

Handling Special Cases

For proper title case formatting, ensure that small words (like "and", "of", "the") remain lowercase.

function titleCase($string) {
    $exceptions = ["and", "of", "the", "in", "to"];
    $words = explode(" ", strtolower($string));

    foreach ($words as $key => $word) {
        if ($key == 0 || !in_array($word, $exceptions)) {
            $words[$key] = ucfirst($word);
        }
    }

    return implode(" ", $words);
}

$text = "a tale of two cities";
echo titleCase($text); // Output: A Tale of Two Cities

4. Capitalizing Only the First Letter with ucfirst()

ucfirst() converts only the first letter of a string to uppercase.

Example: Capitalizing the First Letter

$text = "hello world!";
$capitalized = ucfirst($text);

echo $capitalized; // Output: Hello world!

5. Combining Case Functions for Proper Formatting

Example: Ensuring Proper Sentence Case

To ensure that a sentence starts with an uppercase letter and the rest is lowercase:

$text = "tHIS is A test STRING.";
$sentenceCase = ucfirst(strtolower($text));

echo $sentenceCase; // Output: This is a test string.

6. Comparing Performance of Different Methods

For large text processing, mbstring functions are slightly slower but necessary for non-ASCII text.

Benchmark Test: strtoupper() vs. mb_strtoupper()

$text = str_repeat("hello world! ", 100000); // Large text block

$start = microtime(true);
strtoupper($text);
echo "strtoupper() Time: " . (microtime(true) - $start) . " seconds\n";

$start = microtime(true);
mb_strtoupper($text, "UTF-8");
echo "mb_strtoupper() Time: " . (microtime(true) - $start) . " seconds\n";

Results:

  • strtoupper() is faster for ASCII text.
  • mb_strtoupper() is necessary for UTF-8 but slightly slower.

7. Best Practices for PHP String Case Conversion

✅ Use strtoupper() and strtolower() for ASCII strings.
✅ Use mb_strtoupper() and mb_strtolower() for UTF-8 text.
✅ Use ucwords() for title case but refine it for proper capitalization.
✅ Use ucfirst() when only the first character needs capitalization.
✅ When working with multi-language applications, always use mbstring functions.

Conclusion

String case conversion is essential for data formatting, user input handling, and text presentation. Choosing the right function ensures accurate and readable text output.

This guide covered:

  • Converting text to uppercase using strtoupper() and mb_strtoupper()
  • Lowercasing strings with strtolower() and mb_strtolower()
  • Title case conversion using ucwords() with improvements
  • Handling special cases using ucfirst()

By following best practices, you can effectively manipulate text in PHP while ensuring compatibility across different character sets and languages.

Leave a Reply