PHP 5.4.31 Released

strtoupper

(PHP 4, PHP 5)

strtoupperRenvoie une chaîne en majuscules

Description

string strtoupper ( string $string )

strtoupper() retourne string, après avoir converti tous les caractères alphabétiques en majuscules.

Notez que la notion d'"alphabétique" est déterminée par la configuration de localisation. Cela signifie que pour la configuration par défaut "C", les caractères tels que les voyelles accentuées (comme é, è ou à) ne seront pas converties.

Liste de paramètres

string

La chaîne d'entrée.

Valeurs de retour

Retourne la chaîne en majuscule.

Exemples

Exemple #1 Exemple avec strtoupper()

<?php
$str 
"Marie A un Petit Agneau, et l'aime fORt.";
$str strtoupper($str);
echo 
$str// MARIE A UN PETIT AGNEAU, ET L'AIME FORT.

// Note : Très aurait été converti en TRèS
?>

Notes

Note: Cette fonction gère les chaînes binaires.

Voir aussi

add a note add a note

User Contributed Notes 19 notes

up
7
andre at koethur dot de
1 year ago
One might think that setting the correct locale would do the trick with for example german umlauts, but this is not the case. You have to use mb_strtoupper() instead:

<?php

setlocale
(LC_CTYPE, 'de_DE.UTF8');

echo
strtoupper('Umlaute äöü in uppercase'); // outputs "UMLAUTE äöü IN UPPERCASE"
echo mb_strtoupper('Umlaute äöü in uppercase', 'UTF-8'); // outputs "UMLAUTE ÄÖÜ IN UPPERCASE"

?>
up
5
spaceman at foo dot at
6 years ago
It has been mentioned in a previous comment that all you need to do to let PHP's strtoupper() do the conversion - instead of writing more or less complicated functions yourself - is to specify the locale in which you're doing the case conversion:

<?php setlocale(LC_CTYPE, "de_AT") ?>

It is important to note that setlocale() will silently fail if it can't find the specified locale on your system, so *always* check its return value. Try different spellings: using "de_AT" as an example, there are various combinations that may or may not work for you: "de", "de_AT.utf8", "de_AT.iso-8859-1", "de_AT.latin1", "de_AT@euro", etc).

If you can't find an appropriate locale setting, check your system configuration (locales are a system-wide setting, PHP gets them from the OS). On Windows, locales can be set from the Control Panel; on Linux it depends on your distribution. You can try "sudo dpkg-reconfigure locales" on Debian-based distros, or configure them manually. On Ubuntu Dapper, I had to copy entries over from /usr/share/i18n/SUPPORTED to /var/lib/locales/supported.d/local, then do the dpkg-reconfigure.

After you're done, restart the web server.

That said, there are special cases where you want to do the conversion manually. In German, for example, the letter 'ß' (szlig) only exists as a lower-case character, and so doesn't get converted by strtoupper. The convential way to express a 'ß' in an uppercase string is "SS". This function will take care of this exception (for Latin1 and most of Latin9, at least):

<?php

define
("LATIN1_UC_CHARS", "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ");
define("LATIN1_LC_CHARS", "àáâãäåæçèéêëìíîïðñòóôõöøùúûüý");

function
uc_latin1 ($str) {
   
$str = strtoupper(strtr($str, LATIN1_LC_CHARS, LATIN1_UC_CHARS));
    return
strtr($str, array("ß" => "SS"));
}

?>
up
4
markus dot geiger at mayflower dot de
3 years ago
NOTE: To uppercase a unicode string you simply could use mb_strtoupper($str,  'UTF-8') instead of all that bloated PHP functions ;=)
up
3
mec at stadeleck dot org
11 years ago
something I myself first not thought about:
if there are any html entities (named entities) in your string, strtoupper will turn all letters within this entities to upper case, too. So if you want to manipulate a string with strtoupper it should contain only unicode entities (if ever).
up
3
Justin_Lin at mail2000 dot com dot tw
9 years ago
The following is my code for translate a given string to upper case and it will support chinese traditional :

// 2005/5/30 Justin
// Chinese_Traditional toupper
function CT_to_upper($string)
{       
    $isChineseStart = false;
       
    $new_string = "";
    $i = 0;
    while($i < strlen($string))
    {                  
           if (ord(substr($string,$i,1)) <128)
            {
           if( $isChineseStart == false )
                $new_string .= strtoupper(mb_substr($string,$i,1));
           else      
                $new_string .= substr($string,$i,1);
             }
             else
             {
           if( $isChineseStart == false )
                  $isChineseStart = true;
           else
                $isChineseStart = false;                                 
             $new_string .= substr($string,$i,1);
             }
             $i++;
      }
      return $new_string;         
}
//
up
3
Anonymous
9 years ago
If you only need to extend the conversion by the characters of a certain language, it's possible to control this using an environment variable to change the locale:

setlocale(LC_CTYPE, "de_DE");
up
2
smieat
4 years ago
perfect solutions for turkish utf-8 (including i I conversations):

<?php
function strtolowertr($metin){
    return
mb_convert_case(str_replace('I','ı',$metin), MB_CASE_LOWER, "UTF-8");
}

function
strtouppertr($metin){
    return
mb_convert_case(str_replace('i','İ',$metin), MB_CASE_UPPER, "UTF-8");
}

function
ucwordstr($metin) {
    return
ltrim(mb_convert_case(str_replace(array(' I',' ı', ' İ', ' i'),array(' I',' I',' İ',' İ'),' '.$metin), MB_CASE_TITLE, "UTF-8"));
}

function
ucfirsttr($metin) {
   
$metin = in_array(crc32($metin[0]),array(1309403428, -797999993, 957143474)) ? array(strtouppertr(substr($metin,0,2)),substr($metin,2)) : array(strtouppertr($metin[0]),substr($metin,1));
return
$metin[0].$metin[1];
}
?>
up
1
james at snasta dot ie
2 years ago
In the Irish language certain initial mutations can never be capitalized — the following simple function can be used to capitalize text in Irish.

i.e. Muintir na hÉireann -> MUINTIR NA hÉIREANN

<?php
function strtoupper_ga($a) {
    return
strtr(mb_strtoupper($a, "utf-8"), array(
     
" MB" => " mB",
     
" GC" => " gC",
     
" ND" => " nD",
     
" BHF" => " bhF",
     
" NG" => " nG",
     
" BP" => " bP",
     
" DT" => " dT",
     
" HA" => " hA",
     
" HE" => " hE",
     
" HI" => " hI",
     
" HO" => " hO",
     
" HU" => " hU",
     
" HÁ" => " hÁ",
     
" HÉ" => " hÉ",
     
" HÍ" => " hÍ",
     
" HÓ" => " hÓ",
     
" HÚ" => " hÚ"
   
));
}
?>
up
1
Anonymous
9 years ago
// 2005/5/30 Justin
    // Chinese_Traditional toupper
    function CT_to_upper($string)
    {       
        $isChineseStart = false;
       
          $new_string = "";
         $i = 0;
          while($i < strlen($string))
          {                  
               if (ord(substr($string,$i,1)) <128)
               {
                   if( $isChineseStart == false )
                       $new_string .= strtoupper(mb_substr($string,$i,1));
                   else      
                       $new_string .= substr($string,$i,1);
               }
               else
               {
                   if( $isChineseStart == false )
                       $isChineseStart = true;
                   else
                       $isChineseStart = false;                      
                    
                     $new_string .= substr($string,$i,1);
               }
               $i++;
          }
          return $new_string;         
    }
    //
up
1
dev dot gabiu at gmail dot com
3 years ago
This would be my short version for French characters:

<?php
function fullUpper($string){
  return
strtr(strtoupper($string), array(
     
"à" => "À",
     
"è" => "È",
     
"ì" => "Ì",
     
"ò" => "Ò",
     
"ù" => "Ù",
         
"á" => "Á",
     
"é" => "É",
     
"í" => "Í",
     
"ó" => "Ó",
     
"ú" => "Ú",
         
"â" => "Â",
     
"ê" => "Ê",
     
"î" => "Î",
     
"ô" => "Ô",
     
"û" => "Û",
         
"ç" => "Ç",
    ));
}
?>
up
1
RUNET
7 years ago
Russian

function str_to_upper($str){
    return strtr($str,
    "abcdefghijklmnopqrstuvwxyz".
    "\xE0\xE1\xE2\xE3\xE4\xE5".
    "\xb8\xe6\xe7\xe8\xe9\xea".
    "\xeb\xeC\xeD\xeE\xeF\xf0".
    "\xf1\xf2\xf3\xf4\xf5\xf6".
    "\xf7\xf8\xf9\xfA\xfB\xfC".
    "\xfD\xfE\xfF",
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
    "\xC0\xC1\xC2\xC3\xC4\xC5".
    "\xA8\xC6\xC7\xC8\xC9\xCA".
    "\xCB\xCC\xCD\xCE\xCF\xD0".
    "\xD1\xD2\xD3\xD4\xD5\xD6".
    "\xD7\xD8\xD9\xDA\xDB\xDC".
    "\xDD\xDE\xDF");
}
up
1
willyann at gmail dot com
9 years ago
chinese

function to_upper($string) {
  $new_string = "";
  $i = 0;
  while($i < strlen($string)) {
   if (ord(substr($string,$i,1)) <128)
   {
     $new_string .= strtoupper(substr($string,$i,1));
     $i++;
   } else {
     $new_string .= substr($string,$i,2);
     $i=$i+2;
   }
  }
  return $new_string;
}
up
0
Jaason
5 years ago
convert polish special letters into big and small chars;p

<?php
function toUpper($string) {
    return (
strtoupper(strtr($string, 'ęóąśłżźćń','ĘÓĄŚŁŻŹĆŃ' )));
    };

function
toLower($string) {
    return (
strtolower(strtr($string,'ĘÓĄŚŁŻŹĆŃ', 'ęóąśłżźćń' )));
    };
?>
up
0
chris at table4 dot com
5 years ago
Simple function to change the case of your string and any accented html characters contained within it.

Inspired by fullUpper(), by silent at gmx dot li... just a little bit more atomic.

<?php

function convertCase($str, $case = 'upper')
{
//yours, courtesy of table4.com  :)
 
switch($case)
  {
    case
"upper" :
    default:
     
$str = strtoupper($str);
     
$pattern = '/&([A-Z])(UML|ACUTE|CIRC|TILDE|RING|';
     
$pattern .= 'ELIG|GRAVE|SLASH|HORN|CEDIL|TH);/e';
     
$replace = "'&'.'\\1'.strtolower('\\2').';'"; //convert the important bit back to lower
   
break;
   
    case
"lower" :
     
$str = strtolower($str);
    break;
  }
 
 
$str = preg_replace($pattern, $replace, $str);
  return
$str;
}
?>

Depending on what you are trying to achieve you would call like this:

<?php

//with entities...
$str = convertCase(htmlentities($str, ENT_QUOTES, "ISO-8859-1"));

?>
up
0
Oliv.
6 years ago
accents convertion trick :

<?php
       
   
function ucfirstHTMLentity($matches){
        return
"&".ucfirst(strtolower($matches[1])).";";
    }
    function
fullUpper($str){
       
$subject = strtoupper(htmlentities($str, null, 'UTF-8'));
       
$pattern = '/&([A-Z]+);/';
        return
preg_replace_callback($pattern, "ucfirstHTMLentity", $subject);
    }

        print
fullUpper($_REQUEST["txt"]);
   
?>
up
0
xguimax at gmail dot com
6 years ago
Portuguese version of String Capitalize in PHP.

    function strProper($str)
    {
        $noUp = array('um','uma','o','a','de','do','da','em');
        $str = trim($str);
        $str = strtoupper($str[0]) . strtolower(substr($str, 1));
        for($i=1; $i<strlen($str)-1; ++$i) {
            if($str[$i]==' ') {
                for($j=$i+1; $j<strlen($str) && $str[$j]!=' '; ++$j); //find next space
                $size = $j-$i-1;
                $shortWord = false;
                if($size<=3) {
                    $theWord = substr($str,$i+1,$size);
                    for($j=0; $j<count($noUp) && !$shortWord; ++$j)
                        if($theWord==$noUp[$j])
                            $shortWord = true;
                }
                if( !$shortWord )
                    $str = substr($str, 0, $i+1) . strtoupper($str[$i+1]) . substr($str, $i+2);
            }  
            $i+=$size;
        }
        return $str;
    }
up
0
bart at insane dot at
8 years ago
When using UTF-8 and need to convert to uppercase with
special characters like the german ä,ö,ü (didn't test for french,polish,russian but think it should work, too) try this:

function strtoupper_utf8($string){
    $string=utf8_decode($string);
    $string=strtoupper($string);
    $string=utf8_encode($string);
    return $string;
}
up
0
martine
10 years ago
This may save you time and effort (if you need to convert european languages such as Czech, Portugees, German or Swedish)

the function mb_strtoupper() converts all accented characters in the latin alphabet, ie. , , , etc. This is easier than some of the suggestions below. It should also convert case properly for russian, etc.
up
-2
davaco at net dot hr
2 years ago
Simple function for uppercase encoding of croatian text! :)

<?php
function strtoupper_hr($a) {
    return
strtr(mb_strtoupper($a, "utf-8"), array(
     
" š" => " Š",
     
" đ" => " Đ",
     
" ž" => " Ž",
     
" č" => " Č",
     
" ć" => " Ć"
   
));
}
?>
To Top