strtoupper

(PHP 3, PHP 4, PHP 5)

strtoupper -- Make a string uppercase

Description

string strtoupper ( string string )

Returns string with all alphabetic characters converted to uppercase.

Note that 'alphabetic' is determined by the current locale. For instance, in the default "C" locale characters such as umlaut-a () will not be converted.

例子 1. strtoupper() example

<?php
$str
= "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo
$str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
?>

注: 本函数可安全用于二进制对象。

See also strtolower(), ucfirst(), ucwords() and mb_strtoupper().


add a note add a note User Contributed Notes
my email is trucex and its at gmail
21-Oct-2006 09:55
There are a lot of lengthy examples on doing proper case. If you just need to have the first letter of the string capitalized and the rest lower case, just do this:
<?php
$string
= strtolower($string);
$string = substr_replace($string, strtoupper(substr($string, 0, 1)), 0, 1);
?>

If you need to have all the words in a sentence like that, just set it up like this:
<?php
$words
= explode(" ", $string);
for(
$i=0; $i<count($words); $i++) {
  
$string = strtolower($words[$i]);
  
$string = substr_replace($string, strtoupper(substr($string, 0, 1)), 0, 1);
  
$pString .= "$string ";
}
?>

Obviously, you will have a bit more work ahead of you if you want to do anything fancy, but I didn't need anything fancy and I know there are thousands of applications that don't need anything fancy, so keeping it simple works better. :)
Cory
24-Jul-2006 12:05
This function converts any series of english words to Proper Casing.  It also accounts for words such as 'a' and 'the'.  To change what words are ignored, just change the $noUp array.

function strProper($str) {
   $noUp = array('a','an','of','the','are','at','in');
   $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;
}
sjrd at redaction-developpez dot com
04-Jun-2006 02:12
Angus Lord's function has got a problem with html entities such as &amp;, for they're converted into &Amp;, which is incorrect.

The following code fixes the problem:

<?php
function to_upper($string)
{
 
$new_string = "";
  while (
eregi("^([^&]*)(&)(.)([a-z0-9]{2,9};|&)(.*)", $string, $regs))
  {
  
$entity = $regs[2].strtoupper($regs[3]).$regs[4];
   if (
html_entity_decode($entity) == $entity)
    
$new_string .= strtoupper($regs[1]).$regs[2].$regs[3].$regs[4];
   else
    
$new_string .= strtoupper($regs[1]).$entity;
  
$string = $regs[5];
  }
 
$new_string .= strtoupper($string);
  return
$new_string;
}
?>
bart at insane dot at
10-May-2006 07:31
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;
}
tree2054 at hotmail dot com
15-Feb-2006 02:11
An even simpler version of h3's rewrite:

<?php
function isupper($i) { return (strtoupper($i) === $i);}
function
islower($i) { return (strtolower($i) === $i);}
?>
h3@valleyfield
03-Feb-2006 05:41
I've modified the code of Indio to recreate the C functions isupper and islower.. pretty simple and clean, as you can see;
<?php
function isupper($i) { return (strcmp(strtoupper($i),$i)==0)?true:false; }
function
islower($i) { return (strcmp(strtolower($i),$i)==0)?true:false; }
?>

cheers.
Grkem PAACI(gorkempacaci[et]gmail.com)
06-Jan-2006 10:36
These functions can be used on Turkish(iso-8859-9):
Turkce(iso-8859-9) icin su fonksiyonlar kullanilabilir:

$tr_low_letters = str_split("abcdefghijklmnopqrstuvwxyz");
$tr_up_letters = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
function tr_uppercase($str) {
   global $tr_low_letters, $tr_up_letters;
   return str_replace($tr_low_letters, $tr_up_letters, $str);
}
function tr_lowercase($str) {
   global $tr_low_letters, $tr_up_letters;
   return str_replace($tr_up_letters, $tr_low_letters, $str);
}
function tr_fuppercase($str) {//only first letter uppercase
   return tr_uppercase($str[0]) . tr_lowercase(substr($str,1));
}
Vadim from Baku
27-Nov-2005 05:55
The following function counts uppercase letters in English and Cyrillic. It works great with cyrillic when strtolower doesn't work due to enviroment settings.(Thank you Sean!).

preg_match_all("@[A-Z-]@",$str,$m,PREG_OFFSET_CAPTURE)

It is probably displayed incorrectly due to page encoding, but there are range from the first uppercase letter of the latin alphabet to the last one and range from the first uppercase cyrillic alphabet letter to the last one in the pattern. Not sure but similar approach can work for other alphabets.
Beniamin
26-Nov-2005 10:04
Here is correct str2upper function for polish programmers (plus str2lower function):

<?php
function str2upper($text){
   return
strtr($text,
  
"abcdefghijklmnopqrstuvwxyz".
  
"\xB1\xE6\xEA\xB3\xF1\xF3\xB6\xBC\xBF". // ISO 8859-2
  
"\xB9\x9C\x9F", // win 1250
  
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
  
"\xA1\xC6\xCA\xA3\xD1\xD3\xA6\xAC\xAF". // ISO 8859-2
  
"\xA5\x8C\x8F"  // win 1250
  
);
}

function
str2lower($text){
   return
strtr($text,
  
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
  
"\xA1\xC6\xCA\xA3\xD1\xD3\xA6\xAC\xAF". // ISO 8859-2
  
"\xA5\x8C\x8F"// win 1250
  
"abcdefghijklmnopqrstuvwxyz".
  
"\xB1\xE6\xEA\xB3\xF1\xF3\xB6\xBC\xBF". // ISO 8859-2
  
"\xB9\x9C\x9F" // win 1250
  
);
}
?>
Static Bit
06-Nov-2005 11:35
function UpperPerCent($str)
         {
         $len  = 0;
         $upper = 0;
         for ($i = 0; $i < strlen($str); ++$i) 
             {
             if ($str[$i] <> strtolower($str[$i]))
               {
               ++$len;
               ++$upper;
               }
             if ($str[$i] <> strtoupper($str[$i]))
               {
               ++$len;
               }
             }
            
         $per_cent = 0;
         if ($len > 0)
           $per_cent = ($upper/$len)*100;
            
         return $per_cent;
         }
Chrobak <gni at centrum dot cz>
26-Oct-2005 10:08
My own kind of this function for czechs, there are some special chars, it is n00b function but really useful.

<?php
function tolower($string)
 {
    
$big="ABCDEFGHIJKLMNOPQRSTUVWXYZ&#282;&#268;&#344;";
  
$small="abcdefghijklmnopqrstuvwxyz&#283;&#269;&#345;";
    
$count=strlen($big)-1;
     for(
$i=0;$i<=$count;$i++)
     {
        
$string=str_replace($big[$i],$small[$i],$string);
     }
   return
$string;
 }
 
 function
toupper($string)
 {
    
$big="ABCDEFGHIJKLMNOPQRSTUVWXYZ&#282;&#268;&#344;";
  
$small="abcdefghijklmnopqrstuvwxyz&#283;&#269;&#345;";
    
$count=strlen($small)-1;
     for(
$i=0;$i<=$count;$i++)
     {
        
$string=str_replace($small[$i],$big[$i],$string);
     }
   return
$string;
 }
?>
Sean
20-Sep-2005 09:41
A quick, easy way to see if there are any caps; AND tell how many there are... you could easily do this:

<?
$str
="3 CAPS and the rest lowercase";
$howmany=preg_match_all("@[A-Z]@",$str,$m,PREG_OFFSET_CAPTURE);
?>

That would make $howmany become the integer 4; because there are 4 capitals in the string: $str. ("CAPS")

Hope this helps anyone.

~Sean
Indio
17-Aug-2005 12:13
Here is way to test a string for upper case characters (smaller footprint than previous post)

// test if a string or a single character in a string is uppercase
// if this returns false, then the "string" is lower case
function CheckCase($string,$location='-1') {

   // if a location is passed in, check that character only.
   // if not, or the passed in value is <0, check the whole
   // string for uppercase characters.

   if($location >= 0) {
         return(strcmp(strtolower($string{$location}),$string{$location}));
   }
   else {
         return(strcmp(strtolower($string),$string));
   }
}
ascheinberg at gmail dot com
09-Aug-2005 12:21
I've searched high and low for a function to test whether or not a string is uppercase.  I wrote this little function to do that. 

Set $char equal to the position of the character you want to test, 0 for just the first letter, or -1 to test if there are any caps at all in the string.  This functions returns TRUE if the character(s) are uppercase or FALSE if the output is not uppercase.

function is_uppercase($str,$char='-1') {
   if($char==-1) {
       if(strtolower($str) != $str) {
           return TRUE;
       } else {
           return FALSE;
       }
   } else {
       if(strtolower($str{$char}) != $str{$char}) {
           return TRUE;
       } else {
           return FALSE;
       }       
   }
}

How to Use This function:

$test = is_uppercase("Test",-1); //returns TRUE
$test = is_uppercase("Test",0); //returns TRUE
$test = is_uppercase("Test",1); //returns FALSE
$test = is_uppercase("this is a string!"); //returns FALSE
$test = is_uppercase("this is a string!",-1); //returns FALSE
$test = is_uppercase("this is a string!",5); //returns FALSE
julas
01-Aug-2005 06:46
The code for Polish programmers was spolied a little bit - \xB3 should be turned into \xA3, not the opposite. So the correct code is:

function str2upper($text){
   return strtr($text,
   "abcdefghijklmnopqrstuvwxyz".
   "\xB1\xE6\xEA\xA3\xF1\xF3\xB6\xBC\xBF". // ISO 8859-2
   "\xB9\x9C\x9F", // win 1250
   "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
   "\xA1\xC6\xCA\xB3\xD1\xD3\xA6\xAC\xAF". // ISO 8859-2
   "\xA5\x8C\x8F"  // win 1250
   );
}
topera at gmail dot com
14-Jul-2005 11:52
I'm using the function stringUpDown() (from Jose Roberto Kerne), and I found one problem:

<?
echo stringUpDown("");
// returns "", and it is wrong
// the correct value is:""
?>

To fix it, I changed this:

<?php

  
// begin of the function ......

  
$arrayLower=array(''
  
,'','','','',''
  
,'','','',''
  
,'','','',''
  
,'','','','',''
  
,'','','','');
 
  
$arrayUpper=array(''
  
,'','','','',''
  
,'','','',''
  
,'','','',''
  
,'','','','',''
  
,'','','','');

  
// end of the function ......

?>

to this:

<?php

  
// begin of the function ......

  
$arrayLower=array(''
  
,'','','','',''
  
,'','','',''
  
,'','','',''
  
,'','','','',''
  
,'','','','');
 
  
$arrayUpper=array(''
  
,'','','','',''
  
,'','','',''
  
,'','','',''
  
,'','','','',''
  
,'','','','');

  
// end of the function ......

?>

(I changed the array's position)

Rafael Pereira dos Santos
kirsman
08-Jul-2005 01:37
For polish programmers:

function str2upper($text){
   return strtr($text,
   "abcdefghijklmnopqrstuvwxyz".
   "\xB1\xE6\xEA\xA3\xF1\xF3\xB6\xBC\xBF". // ISO 8859-2
   "\xB9\x9C\x9F", // win 1250
   "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
   "\xA1\xC6\xCA\xB3\xD1\xD3\xA6\xAC\xAF". // ISO 8859-2
   "\xA5\x8C\x8F"  // win 1250
   );
30-May-2005 09:11
// 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;         
   }
   //
Justin_Lin at mail2000 dot com dot tw
30-May-2005 09:09
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;         
}
//
willyann at gmail dot com
25-May-2005 12:31
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;
}
13-Mar-2005 09:08
Ah, the last code were spoiled, here is the fixed one:

<?php

function str_to_upper($str){
   return
strtr($str,
  
"abcdefghijklmnopqrstuvwxyz".
  
"\x9C\x9A\xE0\xE1\xE2\xE3".
  
"\xE4\xE5\xE6\xE7\xE8\xE9".
  
"\xEA\xEB\xEC\xED\xEE\xEF".
  
"\xF0\xF1\xF2\xF3\xF4\xF5".
  
"\xF6\xF8\xF9\xFA\xFB\xFC".
  
"\xFD\xFE\xFF",
  
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
  
"\x8C\x8A\xC0\xC1\xC2\xC3\xC4".
  
"\xC5\xC6\xC7\xC8\xC9\xCA\xCB".
  
"\xCC\xCD\xCE\xCF\xD0\xD1\xD2".
  
"\xD3\xD4\xD5\xD6\xD8\xD9\xDA".
  
"\xDB\xDC\xDD\xDE\x9F");
}

?>

So, this function changes also other letters into uppercase, strtoupper() does only change: a-z to: A-Z.
13-Mar-2005 02:18
This function changes also other letters into uppercase, strtoupper() does only change: a-z to: A-Z.

<?php

function str_to_upper($str){
   return
strtr($str, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}

?>
WM-Joep
13-Dec-2004 02:26
you can make a 'key-generator' with strtoupper too!

function MaakCode($lengte=10) { 
   // De tekens die voorkomen in de code
   $tekens = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); 
   $code = ''; 
      
   for($i = 1; $i <= $lengte; $i++) 
     $code .= $tekens[rand(0,10)]; 

   return strtoupper($code);
31-Oct-2004 06:23
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");
Luke (webmaster--@--zeyus--dot--com)
14-May-2004 04:18
Just a comment on Jose Roberto Kerne's script (which should work perfectly), it should be able to be cut down to this, as str_replace accepts array values (as of ??? php version), the only difference is this should be quicker and obviously array values could be replaced with any characters (or sets of strings) you want to be replaced

<?php
function stringUpDown($text, $up = true){

  
# Convert values from Lower to Upper
  
$arrayLower=array(''
  
,'','','','',''
  
,'','','',''
  
,'','','',''
  
,'','','','',''
  
,'','','','');
  
  
$arrayUpper=array(''
  
,'','','','',''
  
,'','','',''
  
,'','','',''
  
,'','','','',''
  
,'','','','');

if(
$text == ''){
   return
$text;
}

if(
$up != true) {
  
$text=strtolower($text);
  
$text=str_replace($arrayUpper, $arrayLower, $text);
}else{
  
$text=strtoupper($text);
  
$text=str_replace($arrayLower, $arrayUpper, $text);
}
   return(
$text);
}
#end of function
?>
p dot thomas at inlive dot info
03-Apr-2004 08:02
Some bench :

String Copy, OUT=IN : 21.398067474365 ms

String TRANSFORMATION :

- strtolower : 383.09001922607 ms
- strtolower( strtr) : 267.36092567444 ms
- preg_replace : 16624.928951263 ms
- stringUpDown : 4013.0908489227 ms

IN : jehrjzh r''_- &(r&)  EAZREZREZ^m
OUT : jehrjzh r''_- &(r&)  eazrezrez^m

Platform : AMD 1 Ghz, Win2K, EasyPHP
martine
07-Feb-2004 11:41
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.
mec at stadeleck dot org
02-Dec-2002 10:54
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).
urudz at strategma dot bg
22-Apr-2002 12:49
on linux
php gets LC_LOCAL env variable therefor you must set this

export LC_ALL=bg_BG.CP1251
export LANG=bg_BG.CP1251

before starting of apache i have put this to lines in /etc/rc.d/rc.httpd
-----

cat /etc/rc.d/rc.httpd
#!/bin/sh
#
# Start the Apache web server
#

export LC_ALL=bg_BG.CP1251
export LANG=bg_BG.CP1251

case "$1" in
   'start')
     /usr/sbin/apachectl startssl ;;
   'stop')
     /usr/sbin/apachectl stop ;;
   'restart')
     /usr/sbin/apachectl restart ;;
   *)
     echo "usage $0 start|stop|restart" ;;
esac
-------

in windows you must define your "locale"
in control panel  > regional options > general

best regards urudz :>