This site works best with JavaScript enabled!

Description: Text Converter - Lower- / Uppercase Letters

Converter lowercase uppercase letters

This article describes how the text converter "Lower- / Uppercase Letters" works. With this converter, you can transform a text completely into lowercase or uppercase letters.


Not a difficult task

It does not happen very often, but sometimes you get into the situation where you need a text in lowercase or uppercase letters. The task is actually trivial, but in the end you have somehow no idea how to do this without changing all the letters by hand. Therefore, there is this converter: just enter the normal text and convert it. There are also other conversion options available: for example, that only the first letters of all words will be in uppercase.

How does this converter work?

Depending on the conversion option, different functions and commands are executed. A special treatment is always required for the capital ß (ẞ). Most people probably do not even know that this character actually exists. Previously it was common to replace the ß by SS for upper case. Since the beginning of 2008, however, the character exists, which differs minimally from the small ß. Since 2017, this is even part of the official German spelling.

ALL IN UPPERCASE

The conversion of the whole text into uppercase letters is done with the PHP function mb_strtoupper(). Only the small ß must be replaced separately with :

1
2
$output = mb_strtoupper($input, 'UTF-8');
$output = preg_replace('#ß#u', 'ẞ', $output);

all in lowercase

To convert the whole text into lowercase, we need the function mb_strtolower() instead:

1
2
$output = mb_strtolower($input, 'UTF-8');
$output = preg_replace('#ẞ#u', 'ß', $output);

Only Initials Of All Words In Capital Letters

The conversion of all initial letters into uppercase and all the remaining letters into lowercase is done with the callback function preg_replace_callback():

1
2
3
4
5
6
7
8
9
10
$output = preg_replace_callback("#([A-ZÄÖÜßẞ])([A-ZÄÖÜßẞ]*)#ui", function ($matches)
{
   $initial_letter = mb_strtoupper($matches[1], 'UTF-8');
   $initial_letter = preg_replace('#ß#u', 'ẞ', $initial_letter);

   $remaining_letters = mb_strtolower($matches[2], 'UTF-8');
   $remaining_letters = preg_replace('#ẞ#u', 'ß', $remaining_letters);

   return $initial_letter.$remaining_letters;
}, $input);

rEVERSE UPPERCASE/LOWERCASE

If we want to reverse lowercase and uppercase, it is necessary to check for each letter whether it is lowercase of uppercase:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$output = preg_replace_callback("#([A-ZÄÖÜßẞ])#ui", function ($matches)
{
   if (preg_match('#[A-ZÄÖÜ]#u', $matches[1]))
   {
      $output1 = mb_strtolower($matches[1], 'UTF-8');
   }
   elseif (preg_match('#[a-zäöü]#u', $matches[1]))
   {
      $output1 = mb_strtoupper($matches[1], 'UTF-8');
   }
   elseif (preg_match('#ß#u', $matches[1]))
   {
      $output1 = "ẞ";
   }
   else
   {
      $output1 = "ß";
   }

   return $output1;
}, $input);

Only the first letter in a sentence in uppercase

If only the first letter in a sentence should be in uppercase, the conversion is similar to the case in which only the initial letters of all words shall be in capital letters:

1
2
3
4
5
6
7
8
9
10
$output = preg_replace_callback("#([A-ZÄÖÜßẞ])([^\.]*)#ui", function ($matches)
{
   $initial_letter = mb_strtoupper($matches[1], 'UTF-8');
   $initial_letter = preg_replace('#ß#u', 'ẞ', $initial_letter);

   $remaining_letters = mb_strtolower($matches[2], 'UTF-8');
   $remaining_letters = preg_replace('#ẞ#u', 'ß', $remaining_letters);

   return $initial_letter.$remaining_letters;
}, $input);

AlTeRnAtE lOwErCaSe/UpPeRcAsE fOr EvErY lEtTeR

To alternate the lowercase/uppercase for every letter, this can be implemented as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$output = preg_replace_callback("#([A-ZÄÖÜßẞ])#ui", function ($matches)
{
   static $uppercase = true;

   // If the letter shall be in uppercase
   if ($uppercase)
   {
      $output1 = mb_strtoupper($matches[1], 'UTF-8');
      $output1 = preg_replace('#ß#u', 'ẞ', $output1);
      $uppercase = false;
   }
   // If the letter shall be in lowercase
   else
   {
      $output1 = mb_strtolower($matches[1], 'UTF-8');
      $output1 = preg_replace('#ẞ#u', 'ß', $output1);
      $uppercase = true;
   }
   return $output1;
}, $input);

RAndOM lOwERcaSe/UPperCASe fOr EVerY LEttEr

A random lowercase/uppercase for every letter can be achieved in a similar way with the help of a random number generator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$output = preg_replace_callback("#([A-ZÄÖÜßẞ])#ui", function ($matches)
{
   // If the letter shall be in uppercase
   if (mt_rand(0, 1))
   {
      $output1 = mb_strtoupper($matches[1], 'UTF-8');
      $output1 = preg_replace('#ß#u', 'ẞ', $output1);
   }
   // If the letter shall be in lowercase
   else
   {
      $output1 = mb_strtolower($matches[1], 'UTF-8');
      $output1 = preg_replace('#ẞ#u', 'ß', $output1);
   }
   return $output1;
}, $input);
TO THE LOWER- / UPPERCASE CONVERTER
Share this page
Posted on 24.02.2018 | Last modified on 25.04.2021