字符串函数
在线手册:中文 英文
PHP手册

ucfirst

(PHP 4, PHP 5)

ucfirst将字符串的首字母转换为大写

说明

string ucfirst ( string $str )

str 的首字符(如果首字符是字母)转换为大写字母,并返回这个字符串。

注意字母的定义取决于当前区域设定。例如,在默认的 “C” 区域,字符 umlaut-a(?)将不会被转换。

参数

str

输入字符串。

返回值

返回结果字符串。

范例

Example #1 ucfirst() 范例

<?php
$foo 
'hello world!';
$foo ucfirst($foo);             // Hello world!

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

参见


字符串函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 将字符串的首字母转换为大写

用户评论:

drpain at webster dot org dot za (13-Mar-2012 03:48)

<?php

// Custom function returns a string with the first word in a bigger fontsize, with a default of 22px.

function ucsentance($string, $fontSize=22) {

 
// Do some basic processing on the data and find the first space before returning the modified string.
 
$trimmed = trim($string);
 
$getSpace = strpos($trimmed,' ');    
 
$firstWord = substr($trimmed, 0, $getSpace);
 
$firstWord = ucfirst($firstWord);  
 
$afterFirstWord = substr($trimmed, $getSpace);
 
$output = "<b style='font-size: " . $fontSize . "px;'>" . $firstWord . "</b>" . $afterFirstWord;
  return
$output;
}

// Example of it's use
$string = "Hello World, I am a bouncy castle!";
echo
ucsentance($string);
?>

This will yield <b style="font-size: 22px;">Hello</b> World, I am a bouncy castle!

qeremy [atta] gmail [dotta] com (28-Feb-2012 12:41)

A proper Turkish solution;

<?php
function ucfirst_turkish($str) {
   
$tmp = preg_split("//u", $str, 2, PREG_SPLIT_NO_EMPTY);
    return
mb_convert_case(
       
str_replace("i", "?", $tmp[0]), MB_CASE_TITLE, "UTF-8").
       
$tmp[1];
}

$str = "iyilik güzelL?K";
echo
ucfirst($str) ."\n";   // Iyilik güzelL?K
echo ucfirst_turkish($str); // ?yilik güzelL?K
?>

vlknmtn at gmail dot com (18-Aug-2011 12:31)

Turkish solution:

<?php
mb_internal_encoding
("UTF-8");
mb_regex_encoding("UTF-8");

function
tr_ilkbuyuk($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");
   
    return
trim($tr_text);
}

function
tr_ucwords($text)
{
   
$p = explode(" ",$text);
    if(
is_array($p))
    {
       
$tr_text = "";
        foreach(
$p AS $item)
           
$tr_text .= " ".tr_ilkbuyuk($item);
           
        return
trim($tr_text);
    }
    else
        return
tr_ilkbuyuk($text);
}

$deger = "?i?ll?lsdg";

echo
tr_ucwords($deger);

?>

Quicker (10-May-2011 09:53)

if you want to ucfirst for utf8 try this one:

<?php
function ucfirst_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 return
ucfirst($stri);
}
?>

It is quick, not language (but utf8) dependend and does not use any mb-functions such as mb_ucfirst.

chris at bjelleklang dot org (26-Jan-2011 11:03)

For those who want a multibyte-compliant ucfirst() without wanting to mess with encodings, this should do the trick:

<?php
function mb_ucasefirst($str){
   
$str[0] = mb_strtoupper($str[0]);
    return
$str;
}
?>

pete at namecube dot net (12-Apr-2010 03:08)

for anyone wanting to ucfirst each word in a sentence this works for me:

<?php
function ucfirst_sentence($str)
{
    return
preg_replace('/\b(\w)/e', 'strtoupper("$1")', $str);
}
?>

wilfried dot loche at fr dot adp dot com (22-Jan-2010 10:22)

If someone looks for the equivalent on Oracle DB, here it is: INITCAP. Hope this helps!

octavius (27-Sep-2009 01:18)

For lithuanian text with utf-8 encoding I use two functions (thanks [mattalexxpub at gmail dot com] and Svetoslav Marinov)

<?php
function my_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 (
$sentences as $key => $sentence) {
       
$new_string .= ($key & 1) == 0?
           
my_ucfirst(strtolower(trim($sentence))) :
           
$sentence.' '
    }
    return
trim($new_string);
}
?>

bgschool (30-Jul-2009 12:39)

Simple function for use ucfirst with utf-8 encoded cyrylic text

<?php
   
public function capitalize_first($str) {
       
$line = iconv("UTF-8", "Windows-1251", $str); // convert to windows-1251
       
$line = ucfirst($line);
       
$line = iconv("Windows-1251", "UTF-8", $line); // convert back to utf-8
       
       
return $line;
    }
?>

svetoslavm at gmail dot com (20-Nov-2008 03:40)

For some reason this worked for me.

Mac OS 10.5.1
PHP 5.2.6

<?php
  
/**
     * ucfirst UTF-8 aware function
     *
     * @param string $string
     * @return string
     * @see http://ca.php.net/ucfirst
     */
   
function my_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 Marinov
http://slavi.biz

mattalexxpub at gmail dot com (10-Nov-2008 01:10)

This is what I use for converting strings to sentence case:

<?php
function sentence_case($string) {
   
$sentences = preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
   
$new_string = '';
    foreach (
$sentences as $key => $sentence) {
       
$new_string .= ($key & 1) == 0?
           
ucfirst(strtolower(trim($sentence))) :
           
$sentence.' ';
    }
    return
trim($new_string);
}

print
sentence_case('HMM. WOW! WHAT?');

// Outputs: "Hmm. Wow! What?"
?>

prokur.net - there is my email (29-Jun-2008 11:01)

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')) {
    function
mb_ucfirst($string) {
       
$string = mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
        return
$string;
    }
}

?>

it also check is mb support enabled or not

NoName (12-Mar-2008 08:23)

For strings with diactrical marks (umlauts, etc.), consider mb_convert_case().

charliefortune (20-Feb-2008 11:48)

Here's a function to capitalize segments of a name, and put the rest into lower case. You can pass the characters you want to use as delimiters.

i.e. <?php echo nameize("john o'grady-smith"); ?>

returns John O'Grady-Smith

<?php

function nameize($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_char as $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 chunks and capitalize each one.
           
$mend = '';
           
$a_split = explode($temp,$string);
            foreach (
$a_split as $temp2){
               
//capitalize each portion of the string which was separated at a special character
               
$mend .= ucfirst($temp2).$temp;
                }
           
$string = substr($mend,0,-1);
            }   
        }
    return
ucfirst($string);
    }

?>

webmaster at onmyway dot cz (11-Feb-2008 11:31)

Inspired by the lcfirst function a simple mb_lcfirst to cope with multibyte strings:

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

Uwe (26-Jul-2007 04:08)

@adefoor, Ken and Zee

Changing the case can only be done by understanding the text. Take for example "USA", "Sunday", "March", "I am ...", abbreviations like "prob." and so on.

adefoor at gmail dot com (12-Jul-2007 07:57)

Ken and zee

One thing I would do to make this more unviersally work would be to add strtolower() around your $sentence.  Doing this will allow you to convert an all caps text block as well as an all lowercase text block.

<?php

function sentence_cap($impexp, $sentence_split) {
   
$textbad=explode($impexp, $sentence_split);
   
$newtext = array();
    foreach (
$textbad as $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.

?>

Ken Kehler (14-Mar-2007 08:03)

@ zee: this should solve your !, ?, and any punctuations you want to add. It can probably be cleaned up a bit.

<?php

function sentence_cap($impexp, $sentence_split) {
   
$textbad=explode($impexp, $sentence_split);
   
$newtext = array();
    foreach (
$textbad as $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.

?>

zee (31-Jan-2007 12:09)

Another way to capitalize first letter of every sentence in a text, I hope it will help someone. It won't convert non-English characters, though, and ignores sentences ending with ! or ? etc.

<?php

$text
="this is a sentence. this is another sentence.";

$split=explode(". ", $text);
foreach (
$split as $sentence) {
$sentencegood=ucfirst($sentence);
$text=str_replace($sentence, $sentencegood, $text);
}

echo
$text; // This is a sentence. This is another sentence.

?>

Carel at divers information with dotcom (06-Jan-2007 10:55)

I made a small change. Now it takes care of points 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;
}

(26-Oct-2006 03:45)

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 (12-Sep-2006 02:01)

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] = '&nbsp;&nbsp;' . $string[$i];
        }
    }
    $string = implode ('.', $string);
    return $string;
}

Northie (05-Sep-2006 12:39)

Sentence Case:

<?php

function SentenceCase($str) {
   
$sentences = explode(". ",$str);
    for(
$i=0;$i<count($sentences);$i++) {
       
$sentences[$i][0] = strtoupper($sentences[$i][0]);
    }

    return
implode(". ",$sentences);
}

?>

(27-Jul-2006 10:31)

lcfirst - In case you need to get the original string back after a ucfirst.

    function lcfirst( $str ) {
        $str[0] = strtolower($str[0]);
        return $str;
    }

Markus Ernst (31-Mar-2006 08:34)

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
function my_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?ijklmnopqrstuvwxyz".
           
"\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?IJKLMNOPQRSTUVWXYZ".
           
"\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
utf8_encode($str);
    }
}
?>

Bartuc (27-Feb-2006 01:28)

Here is the fixed function for Turkish alphabet..

<?php

function uc_first($str){
  
$str[0] = 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".
  
"\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\xDE\x9F");
   return
$str;
}

?>

Markus Ernst (12-Jan-2006 02:39)

plemieux' function did not work 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 works in my application (PHP 4.3):

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

plemieux (29-Sep-2005 07:05)

Simple multi-bytes ucfirst():

<?php
function my_mb_ucfirst($str) {
   
$fc = mb_strtoupper(mb_substr($str, 0, 1));
    return
$fc.mb_substr($str, 1);
}
?>

info [at] spwdesign [dot] com (23-Jun-2005 08:48)

This is a simple code to get 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 works as well. It strtolowers the text first then places capitals with ucfirst() where it thinks a capital should be placed, at a new sentence. The previous sentence is ended by '. ' then.

<?php
function filter($text){
   
$filters=mysql_query("SELECT word,result FROM filter");
    while(
$filter=mysql_fetch_array($filters)){
       
$text=str_replace($filter[word],$filter[result],strtolower($text));
       
$parts=explode(". ",$text);
        for(
$i=0;$i<count($parts);$i++){
           
$parts[$i]=ucfirst($parts[$i]);
        }
       
$text=implode(". ",$parts);
    }
    return
$text;
}
?>

(13-Mar-2005 01:11)

Ah, the last code were spoiled, here is the fixed one:

<?php

function uc_first($str){
   
$str[0] = 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");
    return
$str;
}

?>

So, this function changes also other letters into uppercase, ucfirst() does only change: a-z to: A-Z.

Stig-Arne Grnroos (25-Dec-2004 06:46)

This function does not work as expected with characters from non-English alphabets (I only tested it with scandinavian letters, => ). It leaves them as lowercase.

Someone already commented that the function doesn't work on html entities, which is somewhat understandable. This bug however takes place before I convert the strings to html.

steven at tux dot appstate dot edu (21-Jun-2004 08:14)

Note: the return for this function changed in versions 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);

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

Results for >4.3:
string(0) "" string(4) "Owen"

Ami Hughes (ami at mistress dot name) (08-Apr-2004 04:34)

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 like Lee's FormatName function in an input box designed for their full name as this script was only designed to check 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 starts 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 quick alternative to writing some bigger 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 comments as not to confuse them with my own.

<?php

  
function FormatName($name=NULL)
   {
       if (empty(
$name))
           return
false;

      
$name = strtolower($name);
      
$name = preg_replace("[\-]", " - ",$name); // Surround hyphens with our delimiter so our strncmp is accurate
      
if (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 delimiter
          
return ucwords($name);
          
       }
   }

?>

lazaro_tolentino at hotmail dot com (01-Apr-2004 09:16)

this is a advance ucfirst function, for upper especifics words, with config in array of seperator
/**
 * @return string
 * @param string $str frase que passar pelo parce
 * @desc Pega uma frase e devolve a mesma com as palavras com suas
 * maiusculas  obedecendo um criterio configurado no array $string_sep
 *
 * @since 2004-04-01 15:04 adicionado a variavel $tring_sep que um
 * array contendo todos os separadores a serem usados
*/
function str_upper_lower($str)
{
    /**
    * array contendo todos os separadores
    */
    $string_sep=array(' ','-','/','_','.');
    /**
    * coloca todas as palavras com letras minusculas
    */
    $str=strtolower($str);
   
    /**
    * testa todos os separadores
    */
    for ($i=0;$i<count($string_sep);$i++)
    {
        $sep=$string_sep[$i];
        /**
        * separa a frase usando os separador atual
        */
        $array_words = explode($sep, $str);
       
        /**
        * variavel que conter o valor temporario
        */
        $tmp_str = '';
        $i2=0;
        foreach ($array_words as $word)
        {
            /**
            * se a quantidade de caracteres for maior que dois, ou se conter ponto,
            *  devolve upper da primeira letra
            */
            $tmp_str .=(strlen($word)>2 || strpos($word,'.')?ucfirst($word):$word);
            /**
            * no adiciona o separador no fim de strings
            */
            if ($i2<count($array_words)-1)
            {
                $tmp_str .= $sep;
            }
            $i2++;
        }
        $str = $tmp_str;
    }
    return $str;
}

Lee Benson (05-Mar-2004 10:37)

Here's a function I threw together when needing to validate name entries (both first name and last name).

This allows simple formatting for names prefixed with "Mc" (like McDonald, McCulloch, etc) and names prefixed with O (like O'Reilly, O'Conner, etc)..

It also allows double-barrelled names to be formatted correctly, in the Smith-Jones way.

Here's the function...

<?php

   
function FormatName($name=NULL) {
       
       
/* Formats a first or last name, and returns the formatted
        version */
       
       
if (empty($name))
            return
false;
           
       
// Initially set the string to lower, to work on it
       
$name = strtolower($name);
           
       
// Run through and uppercase any multi-barrelled names

       
$names_array = explode('-',$name);

        for (
$i = 0; $i < count($names_array); $i++) {
           
           
// "McDonald", "O'Conner"..
           
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]);
   
            }
           
           
// Always set the first letter to uppercase, no matter what
           
$names_array[$i] = ucfirst($names_array[$i]);
           
        }

       
// Piece the names back together
       
$name = implode('-',$names_array);

       
// Return upper-casing on all missed (but required) elements of the $name var
       
return ucwords($name);
       
    }

?>

If you have any other "rules" to follow for international/foreign naming rules, you can add them to the foreach loop, and it should still follow all of the other rules.

It's a quick fix, but it seems to do the job nicely.

Examples...

<?php

$name
= "o'cONNER-MCdOnAld";
echo
FormatName($name);

?>

Returns: O'Conner-McDonald

(04-Mar-2004 05:46)

Of course ucfirst() will _not_ convert html entities such as &uuml; (u-Umlaut as ) to &Uuml; which would represent .

bkimble at ebaseweb dot com (09-Jun-2003 01:02)

Here is a handy function that makes the first letter of everything in a sentence upercase. I used it to deal with titles of events posted on my website ... I've added exceptions for uppercase words and lowercase words so roman numeral "IV" doesn't get printed as "iv" and words like "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); 
}