Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : strtoupper

strtoupper

Make a string uppercase (PHP 4, PHP 5)
string strtoupper ( string string )

Example 2471. 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
?>

Related Examples ( Source code ) » strtoupper


Code Examples / Notes » strtoupper

bart

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


martine

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.


cory

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


görkem paÇaci gorkempacaci et gmail.com

These functions can be used on Turkish(iso-8859-9):
Turkce(iso-8859-9) icin su fonksiyonlar kullanilabilir:
$tr_low_letters = str_split("abcçdefgðhýijklmnoöpqrsþtuüvwxyz");
$tr_up_letters = str_split("ABCÇDEFGÐHIÝJKLMNOÖPQRSÞTUÜVWXYZ");
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));
}


justin_lin

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;  
}
//


vadim from baku

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.


julas

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
  );
}


mec

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


p dot thomas

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


runet

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");
}


xguimax

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


urudz

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 :>


silent

ISO-8859-1 (Latin 1) full with all special characters:
<?php
function fullUpper($str){
  // convert to entities
  $subject = htmlentities($str,ENT_QUOTES);
  $pattern = '/&([a-z])(uml|acute|circ';
  $pattern.= '|tilde|ring|elig|grave|slash|horn|cedil|th);/e';
  $replace = "'&'.strtoupper('\\1').'\\2'.';'";
  $result = preg_replace($pattern, $replace, $subject);
  // convert from entities back to characters
  $htmltable = get_html_translation_table(HTML_ENTITIES);
  foreach($htmltable as $key => $value) {
     $result = ereg_replace(addslashes($value),$key,$result);
  }
  return(strtoupper($result));
}
echo fullUpper("try this: äöüß");
?>
results in
TRY THIS: ÄÖÜß


marcinhacia

In response to strtoupper:
There is a simpler way to change the first letter of a string to uppercase:
<?php
$string='this is a much more simpler way to capitalise the first character of a string';
echo ucfirst($string); // This is a much more...
?>


30-oct-2004 11: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");


beniamin

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


kirsman

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
  );


willyann

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


sjrd

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


tree2054

An even simpler version of h3's rewrite:
<?php
function isupper($i) { return (strtoupper($i) === $i);}
function islower($i) { return (strtolower($i) === $i);}
?>


13-mar-2005 01: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.


oliv.

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"]);

?>


30-may-2005 02: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;  
}
//


16-may-2007 09:40

<?php
$string='this is a simpler way to capitalise the first character of a string';
$string[0]=strtoupper($string[0]);
echo $string; // This is a simpler way...
?>


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker