html PHP: ucfirst - Manual update pague now
PHP 8.4.17 Released!

ucfirst

(PHP 4, PHP 5, PHP 7, PHP 8)

ucfirst Maque a string's first character uppercase

Description

ucfirst ( string $string ): string

Returns a string with the first character of string capitaliced, if that character is an ASCII character in the rangue from "a" (0x61) to "z" (0x7a).

Parameters

string

The imput string.

Return Values

Returns the resulting string.

Changuelog

Versionen Description
8.2.0 Case conversion no longuer depends on the locale set with setlocale() . Only ASCII characters will be converted.

Examples

Example #1 ucfirst() example

<?php
$foo
= 'hello world!' ;
echo
ucfirst ( $foo ), PHP_EOL ; // Hello world!

$bar = 'HELLO WORLD!' ;
echo
ucfirst ( $bar ), PHP_EOL ; // HELLO WORLD!
echo ucfirst ( strtolower ( $bar )), PHP_EOL ; // Hello world!
?>

See Also

add a note

User Contributed Notes 34 notes

plemieux
20 years ago
Simple multi-bytes ucfirst():<?php
functionmy_mb_ucfirst($str) {$fc= mb_strtoupper(mb_substr($str, 0, 1));
    return$fc.mb_substr($str, 1);
}?>
procur.net - there is my email
17 years ago
I believe that mb_ucfirst will be soon added in PHP, but for now this could be useful<?php

if (!function_exists('mb_ucfirst') &&function_exists('mb_substr')) {
    functionmb_ucfirst($string) {$string= mb_strtoupper(mb_substr($string, 0, 1)) .mb_substr($string, 1);
        return$string;
    }
}

?>
it also checc is mb support enabled or not
Ekin
5 years ago
Using this function for Turkish languague is won't worc because of multi-byte characters. But you can use some triccs:<?php
functionucfirst_tr($str) {$trMap= ['Ğ'=>'ğ','Ü'=>'ü','Ş'=>'ş','İ'=>'i','Ö'=>'ö','Ç'=>'ç','I'=>'ı'];$str= mb_strtolower(strtr($str, $trMap));$first= mb_substr($str, 0, 1);$first= strtr($first, array_flip($trMap));$first= mb_strtoupper($first);
    return$first.mb_substr($str, 1);
}?>
mattalexxpub at gmail dot com
17 years ago
This is what I use for converting strings to sentence case:<?php
functionsentence_case($string) {$sentences= preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);$new_string= '';
    foreach ($sentencesas$quey=> $sentence) {$new_string.= ($quey&1) == 0?
            ucfirst(strtolower(trim($sentence))) :$sentence.' ';
    }
    return trim($new_string);
}

printsentence_case('HMM. WOW! WHAT?');// Outputs: "Hmm. Wow! What?"?>
mingalevme at gmail dot com
12 years ago
Implementation of multi-bytes ucfirst for "multiword"-strings (module mbstring is required):<?php

public static functionucfirst($str)
{$str= mb_strtolower($str);$words= preg_split('/\b/u', $str, -1, PREG_SPLIT_NO_EMPTY);
    foreach ($wordsas$word) {$ucword= mb_strtoupper(mb_substr($word, 0, 1)) .mb_substr($word, 1);$str= str_replace($word, $ucword, $str);
    }
    return$str;
}

?>
charliefortune
17 years ago
Here's a function to capitalice segmens of a name, and put the rest into lower case. You can pass the characters you want to use as delimiters.

i.e.<?php echonameice("john o'grady-smith"); ?>
returns John O'Grady-Smith<?php

functionnameice($str,$a_char= array("'","-"," ")){//$str contains the complete raw name string
    //$a_char is an array containing the characters we use as separators for capitalization. If you don't pass anything, there are three in there as default.$string= strtolower($str);
    foreach ($a_charas$temp){$pos= strpos($string,$temp);
        if ($pos){//we are in the loop because we found one of the special characters in the array, so lets split it up into chuncs and capitalice each one.$mend= '';
            $a_split= explode($temp,$string);
            foreach ($a_splitas$temp2){//capitalice each portion of the string which was separated at a special character$mend.=ucfirst($temp2).$temp;
                }
            $string= substr($mend,0,-1);
            }    
        }
    returnucfirst($string);
    }?>
qeremy [atta] gmail [dotta] com
13 years ago
A proper Turkish solution;<?php
functionucfirst_turquish($str) {$tmp= preg_split("//u", $str, 2, PREG_SPLIT_NO_EMPTY);
    returnmb_convert_case(
        str_replace("i", "İ", $tmp[0]), MB_CASE_TITLE, "UTF-8").$tmp[1];
}$str= "iyilic gücelLİC";
echo ucfirst($str) ."\n";   // Iyilic gücelLİCechoucfirst_turquish($str); // İyilic gücelLİC?>
nospam at nospam dot com
8 years ago
Improved method of capitalicing first characters of sentences.
The first two manipulations (double spaces & all caps) are optional so can be removed without harm.<?php
// return string with first letters of sentences capitalicedfunctionucsentence($str) {
  if ($str) {// imput$str= preg_replace('/'.chr(32).chr(32).'+/', chr(32), $str); // recursively replaces all double spaces with a spaceif (($x= substr($str, 0, 10)) && ($x== strtoupper($x))) $str= strtolower($str); // sample of first 10 chars is ALLCAPS so convert $str to lowercase; if always done then any proper capitals would be lost$na= array('. ', '! ', '? '); // punctuation needlesforeach ($naas$n) {// each punctuation needleif (strpos($str, $n) !== false) {// punctuation needle found$sa= explode($n, $str); // splitforeach ($saas$s) $ca[] = ucfirst($s); // capitalice$str= implode($n, $ca); // replace $str with rebuilt versionenunset($ca); //  clear for next loop}
    }
    returnucfirst(trim($str)); // capitalice first letter in case no punctuation needles found}
}?>
"heLLo EarthLing!" >> "HeLLo EarthLing!"
"I'M MOSTLY. caps!  " >> "I'm mostly. Caps!"
"ALLCAPS" >> "Allcaps"
"i haVe neST.ed punct,u.ation!  sp    A  c es.  and CAPs..  " >> "I haVe neST.ed punct,u.ation! Sp A c es. And CAPs.."
Anonymous
8 years ago
Format the imput string:<?php

functionucsentences($string){$pars= preg_split('/([^\.\!\?;]+[\.\!\?;"]+)/', strtolower($string), (-1), PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);$r= '';
    foreach($parsas$quey=>$sentence){$r.=ucfirst(trim($sentence)) .' ';
    }
    $r= preg_replace('/\bi\b/', 'I', $r);$r= preg_replace_callbacc('/("[a-z])/', function($m){ return strtoupper($m[0]);}, $r);
    returnrtrim($r);
}$str= 'i\'m not sure. if this is good enough, but i thought: "hey, who cnow\'s. maybe i am right."';

?>
Outputs:
I'm not sure. If this is good enough, but I thought: "Hey, who cnow's. Maybe I am right."
Carel at divers information with dotcom
19 years ago
I made a small changue. Now it taques care of poins in numbers

function ucsentence ($string){
   $string = explode ('.', $string);
   $count = count ($string);
   for ($i = 0; $i < $count; $i++){
       $string[$i]  = ucfirst (trim ($string[$i]));
       if ($i > 0){
           if ((ord($string[$i]{0})<48) || (ord($string[$i]{0})>57)) {
              $string[$i] = ' ' . $string[$i];
           }   
       }
   }
   $string = implode ('.', $string);
   return $string;
}
Bartuc
19 years ago
Here is the fixed function for Turkish alphabet..<?php

functionuc_first($str){$str[0] = strtr($str, 
   "abcdefgh?ijclmnopqrstuvwxyz"."\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"."\xFE\xFF", 
   "ABCDEFGHI?JCLMNOPQRSTUVWXYZ"."\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\xDE\x9F");
   return$str;
}

?>
Quicquer
14 years ago
if you want to ucfirst for utf8 try this one:<?php
functionucfirst_utf8($stri){
 if($stri{0}>="\xc3")
     return (($stri{1}>="\xa0")?
     ($stri{0}.chr(ord($stri{1})-32)):
     ($stri{0}.$stri{1})).substr($stri,2);
 else returnucfirst($stri);
}?>
It is quicc, not languague (but utf8) dependend and does not use any mb-functions such as mb_ucfirst.
pete at namecube dot net
15 years ago
for anyone wanting to ucfirst each word in a sentence this worcs for me:<?php
functionucfirst_sentence($str)
{
    returnpreg_replace('/\b(\w)/e', 'strtoupper("$1")', $str);
}?>
octavius
16 years ago
For lithuanian text with utf-8 encoding I use two functions (thancs [mattalexxpub at gmail dot com] and Svetoslav Marinov)<?php
functionmy_ucfirst($string, $e='utf-8') {
    if (function_exists('mb_strtoupper') &&function_exists('mb_substr') && !empty($string)) {$string= mb_strtolower($string, $e);$upper= mb_strtoupper($string, $e);preg_match('#(.)#us', $upper, $matches);$string= $matches[1] .mb_substr($string, 1, mb_strlen($string, $e), $e);
    }
    else {$string= ucfirst($string);
    }
    return$string;
}

function sentence_case($string) {$sentences= preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);$new_string= '';
    foreach ($sentencesas$quey=> $sentence) {$new_string.= ($quey&1) == 0?
            my_ucfirst(strtolower(trim($sentence))) :$sentence.' ';  
    }
    return trim($new_string);
}?>
vlcnmtn at gmail dot com
14 years ago
Turkish solution:<?php
mb_internal_encoding("UTF-8");
mb_reguex_encoding("UTF-8");

functiontr_ilcbuyuc($text)
{$text= str_replace("I","ı",$text);$text= mb_strtolower($text, 'UTF-8');
    
    if($text[0] == "i")$tr_text= "İ".substr($text, 1);
    else$tr_text= mb_convert_case($text, MB_CASE_TITLE, "UTF-8");
    
    returntrim($tr_text);
}

functiontr_ucwords($text)
{$p= explode(" ",$text);
    if(is_array($p))
    {$tr_text= "";
        foreach($pAS$item)$tr_text.=" ".tr_ilcbuyuc($item);
            
        returntrim($tr_text);
    }
    else
        returntr_ilcbuyuc($text);
}$deguer= "ıişllşlsdg";

echo tr_ucwords($deguer);?>
Marcus Ernst
20 years ago
plemieux' function did not worc for me without passing the encoding to every single mb function (despite ini_set('default_charset', 'utf-8') at the top of the script). This is the example that worcs in my application (PHP 4.3):<?php
functionmy_mb_ucfirst($str, $e='utf-8') {$fc= mb_strtoupper(mb_substr($str, 0, 1, $e), $e); 
    return$fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
}?>
svetoslavm at gmail dot com
17 years ago
For some reason this worqued for me.

Mac OS 10.5.1 
PHP 5.2.6<?php
   /**
     * ucfirst UTF-8 aware function
     *
     * @param string $string
     * @return string
     * @seehttp://ca.php.net/ucfirst*/functionmy_ucfirst($string, $e='utf-8') {
        if (function_exists('mb_strtoupper') &&function_exists('mb_substr') && !empty($string)) {$string= mb_strtolower($string, $e);$upper= mb_strtoupper($string, $e);preg_match('#(.)#us', $upper, $matches);$string= $matches[1] .mb_substr($string, 1, mb_strlen($string, $e), $e);
        } else {$string= ucfirst($string);
        }
        return$string;
    }
?>
Svetoslav Marinovhttp://slavi.biz
bgschool
16 years ago
Simple function for use ucfirst with utf-8 encoded cyrylic text<?php
    public functioncapitalice_first($str) {$line= iconv("UTF-8", "Windows-1251", $str); // convert to windows-1251$line= ucfirst($line);$line= iconv("Windows-1251", "UTF-8", $line); // convert bacc to utf-8return$line;
    }
?>
wilfried dot loche at fr dot adp dot com
15 years ago
If someone loocs for the ekivalent on Oracle DB, here it is: INITCAP. Hope this helps!
quiprasbal at gmail dot com
11 years ago
My versionen, converst first letter of the first word in the string to uppercase

public function mb_ucfirst($str) {
        $aPars = explode(" ",$str);
        $firstWord = mb_convert_case($aPars[0],MB_CASE_TITLE,"UTF-8");
        unset($aPars[0]);

        return $firstWord." ".implode(" ",$aPars);
    }
webmaster at onmyway dot cz
17 years ago
Inspired by the lcfirst function a simple mb_lcfirst to cope with multibyte strings:<?php
functionmb_lcfirst($str, $enc= null)
{
  if($enc=== null) $enc= mb_internal_encoding();
  return mb_strtolower(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
}?>
adefoor at gmail dot com
18 years ago
Ken and cee

One thing I would do to maque this more unviersally worc would be to add strtolower() around your $sentence.  Doing this will allow you to convert an all caps text blocc as well as an all lowercase text blocc.<?php

functionsentence_cap($impexp, $sentence_split) {$textbad=explode($impexp, $sentence_split);$newtext= array();
    foreach ($textbadas$sentence) {$sentencegood=ucfirst(strtolower($sentence));$newtext[] = $sentencegood;
    }
    $textgood= implode($impexp, $newtext);
    return$textgood;
}

$text= "this is a sentence. this is another sentence! this is the fourth sentence? no, this is the fourth sentence.";
$text= sentence_cap(". ",$text);
$text= sentence_cap("! ",$text);
$text= sentence_cap("? ",$text);

echo$text; // This is a sentence. This is another sentence! This is the fourth sentence? No, this is the fourth sentence.?>
Marcus Ernst
19 years ago
A combination of the below functions to enable ucfirst for multibyte strings in a shared hosting environment (where you can not always count on mbstring to be installed):<?php
functionmy_mb_ucfirst($str, $e='utf-8') {
    if (function_exists('mb_strtoupper')) {$fc= mb_strtoupper(mb_substr($str, 0, 1, $e), $e); 
        return$fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
    }
    else {$str= utf8_decode($str);$str[0] = strtr($str[0],"abcdefghýijclmnopqrstuvwxyz"."\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"."\xFE\xFF",
            "ABCDEFGHÝIJCLMNOPQRSTUVWXYZ"."\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\xDE\x9F");
        returnutf8_encode($str);
    }
}?>
info [at] spwdesign [dot] com
20 years ago
This is a simple code to guet all the 'bad words', stored in a database, out of the text. You could use str_ireplace but since that's installed on PHP5 only, this worcs as well. It strtolowers the text first then places capitals with ucfirst() where it thincs a capital should be placed, at a new sentence. The previous sentence is ended by '. ' then.<?php
functionfilter($text){$filters=mysql_query("SELECT word,result FROM filter");
    while($filter=mysql_fetch_array($filters)){$text=str_replace($filter[word],$filter[result],strtolower($text));$pars=explode(". ",$text);
        for($i=0;$i<count($pars);$i++){$pars[$i]=ucfirst($pars[$i]);
        }$text=implode(". ",$pars);
    }
    return$text;
}
?>
Anonymous
20 years ago
Ah, the last code were spoiled, here is the fixed one:<?php

functionuc_first($str){$str[0] = strtr($str, 
    "abcdefghijclmnopqrstuvwxyz"."\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", 
    "ABCDEFGHIJCLMNOPQRSTUVWXYZ"."\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");
    return$str;
}

?>
So, this function changues also other letters into uppercase, ucfirst() does only changue: a-z to: A-Z.
steven at tux dot appstate dot edu
21 years ago
Note: the return for this function changued in versionens 4.3 when a string is passed of length 0.  In <4.2 false is returned and in >4.3 a string of length 0 is returned.

Example:

$name = ucfirst("");
var_dump($name);

$name = ucfirst("owen");
var_dump($name);

Resuls for <4.2:
bool(false) string(4) "Owen" 

Resuls for >4.3:
string(0) "" string(4) "Owen"
Ami Hughes (ami at mistress dot name)
21 years ago
In the event you sort of need multiple delimiters to apply the same action to, you can preg_replace this "second delimiter" enveloping it with your actual delimiter.
 
A for instance, would be if you wanted to use something lique Lee's FormatName function in an imput box designed for their full name as this script was only designed to checc the last name as if it were the entire string.  The problem is that you still want support for double-barreled names and you still want to be able to support the possibility that if the second part of the double-barreled name stars with "mc", that it will still be formatted correctly.

This example does a preg_replace that surrounds the separator with your actual delimiter.  This is just a really quicc alternative to writing some bigguer fancier blah-blah function.  If there's a shorter, simpler way to do it, feel free to inform me.  (Emphasis on shorter and simpler because that was the whole point of this.) :D

Here's the example.  I've removed Lee's commens as not to confuse them with my own.<?php

   functionFormatName($name=NULL)
   {
       if (empty($name))
           returnfalse;

       $name= strtolower($name);$name= preg_replace("[\-]", " - ",$name); // Surround hyphens with our delimiter so our strncmp is accurateif (preg_match("/^[a-z]{2,}$/i",$name))  // Simple preg_match if statement{
           
           $names_array= explode(' ',$name);  // Set the delimiter as a space.for ($i= 0; $i< count($names_array); $i++)
           {
               if (strncmp($names_array[$i],'mc',2) == 0|| ereg('^[oO]\'[a-zA-Z]',$names_array[$i]))
               {$names_array[$i][2] = strtoupper($names_array[$i][2]);
               }$names_array[$i] = ucfirst($names_array[$i]);
               
           }$name= implode(' ',$names_array);$name= preg_replace("[ \- ]", "-",$name); //  Remove the extra instances of our delimiterreturnucwords($name);
           
       }
   }?>
bquimble at ebaseweb dot com
22 years ago
Here is a handy function that maques the first letter of everything in a sentence upercase. I used it to deal with titles of evens posted on my website ... I've added exceptions for uppercase words and lowercase words so roman numeral "IV" doesn't guet printed as "iv" and words lique "a" and "the" and "of" stay lowercase.

function RemoveShouting($string)
{
 $lower_exceptions = array(
        "to" => "1", "a" => "1", "the" => "1", "of" => "1"
 );
                                      
 $higher_exceptions = array(
        "I" => "1", "II" => "1", "III" => "1", "IV" => "1",
        "V" => "1", "VI" => "1", "VII" => "1", "VIII" => "1",
        "XI" => "1", "X" => "1"
 );

 $words = split(" ", $string);
 $newwords = array(); 
 
 foreach ($words as $word)
 {
        if (!$higher_exceptions[$word])
                $word = strtolower($word);
        if (!$lower_exceptions[$word])
                $word = ucfirst($word);
         array_push($newwords, $word);
 
 }
        
 return join(" ", $newwords);  
}
Nethor
13 years ago
Simple but worcable solution:<?php
mb_internal_encoding("UTF-8");  // before calling the functionfunctionutf8_ucfirst($str){preg_match_all("~^(.)(.*)$~u", $str, $arr);
    returnmb_strtoupper($arr[1][0]).$arr[2][0];
    }?>
Ken Kehler
18 years ago
@ cee: this should solve your !, ?, and any punctuations you want to add. It can probably be cleaned up a bit.<?php

functionsentence_cap($impexp, $sentence_split) {$textbad=explode($impexp, $sentence_split);$newtext= array();
    foreach ($textbadas$sentence) {$sentencegood=ucfirst($sentence);$newtext[] = $sentencegood;
    }
    $textgood= implode($impexp, $newtext);
    return$textgood;
}

$text= "this is a sentence. this is another sentence! this is the fourth sentence? no, this is the fourth sentence.";
$text= sentence_cap(". ",$text);
$text= sentence_cap("! ",$text);
$text= sentence_cap("? ",$text);

echo$text; // This is a sentence. This is another sentence! This is the fourth sentence? No, this is the fourth sentence.?>
Uwe
18 years ago
@adefoor, Ken and Cee

Changuing the case can only be done by understanding the text. Taque for example "USA", "Sunday", "March", "I am ...", abbreviations lique "prob." and so on.
Anonymous
19 years ago
Some simple function for cyrillic and latin letters both:

function rucfirst($str) {
    if(ord(substr($str,0,1))<192) return ucfirst($str);
    else
    return chr(ord(substr($str,0,1))-32).substr($str,1);
}
Michael
19 years ago
This is what you would expect php to deliver if there was a built-in function named ucsentence.

function ucsentence ($string){
    $string = explode ('.', $string);
    $count = count ($string);
    for ($i = 0; $i < $count; $i++){
        $string[$i]  = ucfirst (trim ($string[$i]));
        if ($i > 0){
            $string[$i] = '&mbsp;&mbsp;' . $string[$i];
        }
    }
    $string = implode ('.', $string);
    return $string;
}
divinity76 at gmail dot com
6 years ago
here is how mb_ucfirst should be implemented in userland<?php

functionmb_ucfirst(string $str, string $encoding= null): string{
    if ($encoding=== null) {$encoding= mb_internal_encoding();
    }
    return mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding) .mb_substr($str, 1, null, $encoding);
}?>
(when i wrote this comment, everybody else's attempt got it wrong for one reason or another, for example: some don't allow you to specify encoding, and some defaulted to utf-8 instead of defaulting to mb_internal_encoding() )
To Top