PHP Doku:: Wandelt jeden ersten Buchstaben eines Wortes innerhalb eines Strings in einen Großbuchstaben - function.ucwords.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzTextverarbeitungZeichenkettenString-Funktionenucwords

Ein Service von Reinhard Neidl - Webprogrammierung.

String-Funktionen

<<ucfirst

vfprintf>>

ucwords

(PHP 4, PHP 5)

ucwordsWandelt jeden ersten Buchstaben eines Wortes innerhalb eines Strings in einen Großbuchstaben

Beschreibung

string ucwords ( string $str )

Gibt einen String zurück, in dem das erste Zeichen eines jeden Wortes innerhalb von str in einen Großbuchstaben umgewandelt wurde, sofern es sich dabei um Buchstaben handelt.

Als Wort wird hierbei eine Zeichenkette verstanden, die einem Whitespace (Leerzeichen, Seitenvorschub, Zeilenvorschub, Wagenrücklauf sowie horizontalem und vertikalem Tabulatorzeichen) folgt.

Parameter-Liste

str

Die Eingabezeichenkette.

Rückgabewerte

Gibt die modifizierte Zeichenkette zurück.

Beispiele

Beispiel #1 ucwords()-Beispiel

<?php
$foo 
'hallo welt!';
$foo ucwords ($foo);          // Hallo Welt!

$bar 'HALLO WELT!';
$bar ucwords($bar);             // HALLO WELT!
$bar ucwords(strtolower($bar)); // Hallo Welt!
?>

Anmerkungen

Hinweis: Diese Funktion ist binary safe.

Siehe auch

  • strtoupper() - Wandelt alle Zeichen eines Strings in Großbuchstaben um
  • strtolower() - Setzt einen String in Kleinbuchstaben um
  • ucfirst() - Verwandelt das erste Zeichen eines Strings in einen Großbuchstaben
  • mb_convert_case() - Perform case folding on a string


47 BenutzerBeiträge:
- Beiträge aktualisieren...
jmarois at ca dot ibm dot com
12.02.2010 17:51
My quick and dirty ucname (Upper Case Name) function.

<?php
//FUNCTION

function ucname($string) {
   
$string =ucwords(strtolower($string));

    foreach (array(
'-', '\'') as $delimiter) {
      if (
strpos($string, $delimiter)!==false) {
       
$string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return
$string;
}
?>
<?php
//TEST

$names =array(
 
'JEAN-LUC PICARD',
 
'MILES O\'BRIEN',
 
'WILLIAM RIKER',
 
'geordi la forge',
 
'bEvErly CRuSHeR'
);
foreach (
$names as $name) { print ucname("{$name}\n"); }

//PRINTS:
/*
Jean-Luc Picard
Miles O'Brien
William Riker
Geordi La Forge
Beverly Crusher
*/
?>

You can add more delimiters in the for-each loop array if you want to handle more characters.
Kovik
27.12.2009 16:37
ucwords() only uses whitespace as a delimiter. I found that I needed the ability to use other delimiters for denoting words.

Example: Transforming "content-type" into "Content-Type".

<?php
/**
 * Capitalize all words
 * @param string Data to capitalize
 * @param string Word delimiters
 * @return string Capitalized words
 */
function capitalizeWords($words, $charList = null) {
   
// Use ucwords if no delimiters are given
   
if (!isset($charList)) {
        return
ucwords($words);
    }
   
   
// Go through all characters
   
$capitalizeNext = true;
   
    for (
$i = 0, $max = strlen($words); $i < $max; $i++) {
        if (
strpos($charList, $words[$i]) !== false) {
           
$capitalizeNext = true;
        } else if (
$capitalizeNext) {
           
$capitalizeNext = false;
           
$words[$i] = strtoupper($words[$i]);
        }
    }
   
    return
$words;
}
?>

Testing:
<?php
echo capitalizeWords('this is a test');  // This Is A Test
echo capitalizeWords('this-is-a-test');  // This-is-a-test
echo capitalizeWords('this is a test', ' ');  // This Is A Test
echo capitalizeWords('this-is-a-test', '-');  // This-Is-A-Test
echo capitalizeWords('this-is a-test', ' ');  // This-is A-test
echo capitalizeWords('this-is a-test', '-');  // This-Is a-Test
echo capitalizeWords('this-is a-test', ' -');  // This-Is A-Test
?>
mariodivece at yahoo dot com
26.11.2009 6:04
Here's a simple function to convert a pascal cased, camel cased or underscore-separated string (usually for identifiers) that works well. It'd be nice if someone could provide the regex equivalent.

<?php

class StringHelper
{
    const
CONVENTION_AUTO = 0;
    const
CONVENTION_PASCAL = 1;
    const
CONVENTION_CAMEL = 2;
    const
CONVENTION_USCORE = 3;

   
   
/**
     * Converts a camel-cased, pascal-cased or underscore-separated word into a human-readable string
     * @param string $str
     * @param int $convention
     * @return string
     */
   
public static function toWords($str, $convention = self::CONVENTION_AUTO)
    {
        if (
$convention === self::CONVENTION_AUTO)
        {
            if (
self::strContains($str, '_'))
            {
               
$convention = self::CONVENTION_USCORE;
            }
            else
            {
               
$convention = self::CONVENTION_PASCAL;
            }
        }
       
        if (
$convention === self::CONVENTION_USCORE)
        {
            return
str_replace('_', ' ', $str);
        }
       
        if (
$convention === self::CONVENTION_PASCAL || $convention === self::CONVENTION_CAMEL)
        {
           
$baseStr = ucfirst($str);
           
$whitespaceBefore = strtoupper($str) . "01234567890";
           
           
$retStr = "";
            for (
$i = 0; $i < strlen($baseStr); $i++)
            {
               
$chr = $baseStr[$i];
                if (
strstr($whitespaceBefore, $chr) !== false)
                {
                   
$retStr .= " " . $chr;
                }
                else
                {
                   
$retStr .= $chr;
                }
            }
           
            return
trim($retStr);
        }
        else
        {
            throw new
Exception(692, '$convention must be one of the CONVENTION-prefixed constants');
        }
       
    }

}

?>
robert at broofa dot com
8.07.2009 15:42
Some recipes for switching between underscore and camelcase naming:

<?php
// underscored to upper-camelcase
// e.g. "this_method_name" -> "ThisMethodName"
preg_replace('/(?:^|_)(.?)/e',"strtoupper('$1')",$string);

// underscored to lower-camelcase
// e.g. "this_method_name" -> "thisMethodName"
preg_replace('/_(.?)/e',"strtoupper('$1')",$string);

// camelcase (lower or upper) to underscored
// e.g. "thisMethodName" -> "this_method_name"
// e.g. "ThisMethodName" -> "this_method_name"
strtolower(preg_replace('/([^A-Z])([A-Z])/', "$1_$2", $string));
?>

Of course these aren't 100% symmetric.  For example...
  * this_is_a_string -> ThisIsAString -> this_is_astring
  * GetURLForString -> get_urlfor_string -> GetUrlforString
Greg S
26.04.2009 18:16
I did the same thing as Catalin, but for French names.

Here's what I'm doing :

For each word (not considering the single-quote as a word boundary character) :
- Put the word in lower case
- If the word is "de", return, else, put the first letter in upper-case
- See if the second character of the word is a single-quote
- Yes ? Put the next char in upper case
- And if the char before the single quote is D, put it back to lower case (-> d)

This complies with the French rules for capitalization in names.

Sample results :
-d'Afoo Bar
-de Foo Bar
-O'Foo Bar
-Foo'bar

<?php
function my_ucwords($s) {
       
$s = preg_replace_callback("/(\b[\w|']+\b)/s", fixcase_callback, $s);
       
        return
$s;        
       
    }
   
    function
fixcase_callback($word) {

       
$word = $word[1];
       
       
$word = strtolower($word);
       
        if(
$word == "de")
            return
$word;
       
       
$word = ucfirst($word);
       
        if(
substr($word,1,1) == "'") {
            if(
substr($word,0,1) == "D") {
               
$word = strtolower($word);
            }
           
$next = substr($word,2,1);
           
$next = strtoupper($next);
           
$word = substr_replace($word, $next, 2, 1);
        }
       
        return
$word;
    }
?>
vista_ at live dot se
17.11.2008 11:02
<?php
     
if(!function_exists('mb_ucwords'))
      {
            function
mb_ucwords($str)
            {
                  return
mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
            }
      }
?>
dyer85 at gmail dot com
7.11.2008 12:33
When attempting to adopt a solution similar to Catalin's post (20-Oct-2008 10:14), I ran into some additional problems. Just a heads up, Catalin's wasn't capitalizing correctly when a name like "O'reilley" appeared at the start of the string or a new line. Also, it doesn't account for locale-sensitivity.

I also needed to recognize additional Irish surnames, such as "McArthur/MacArthur" or "FitzGerald". I also didn't want to misinterpret French as an Irish surname, so, "S'il vous plaît" shouldn't result in the "i" in "S'il" capitalized.

I modified Catalin's version, but it's still not perfect. This version happened to suit my needs, so be sure to assess your own needs before using.

<?php

function my_ucwords($str)
{
   
$str = ucwords($str);

   
// Not perfect
   
return preg_replace(
       
'/
            (?: ^ | \\b )         # assertion: beginning of string or a word boundary
            ( O\' | Ma?c | Fitz)  # attempt to match Irish surnames
            ( [^\W\d_] )          # match next char; we exclude digits and _ from \w
        /xe'
,
       
"'\$1' . strtoupper('\$2')",
       
$str);
}

?>
strazds at gmail dot com
1.11.2008 11:45
ucwords for UTF-8 strings:

<?php
function mb_ucwords($str) {
   
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
    return (
$str);
}
?>
Catalin
20.10.2008 16:14
I have looked for a work-around that would upper-case letter after an ' too.

For example,

ucword("o'lunney's");

would output "O'lunney's" and I wanted it to output "O'Lunney's".

Here is my function:

<?php
function my_ucwords($string) {
       
$string = ucwords(strtolower($string));
       
$string = preg_replace_callback("/( [ a-zA-Z]{1}')([a-zA-Z0-9]{1})/s",create_function('$matches','return $matches[1].strtoupper($matches[2]);'),$string);
        return
$string;
}
?>

Hope it helps someone.
Alex
27.08.2008 18:43
A modified sentenceNormalizer by gregomm
 
Features:
1- Removes duplicated question marks, exclamations and periods
2- Capitalize first letter of a sentence.
3- Split sentences not only with "." but also with "?" and "!"
4- Puts a white space at the end of each sentence
5- Retains newlines

--removed from orginal function--
undestand the meaning of "¡" and "¿" in languages like spanish.
undestand the htmlentitity version of this simbols.
--removed from orginal function--

<?php
function sentenceNormalizer($sentence_split) {
   
$sentence_split = preg_replace(array('/[!]+/','/[?]+/','/[.]+/'),
                                   array(
'!','?','.'),$sentence_split);       
   
   
$textbad = preg_split("/(\!|\.|\?|\n)/", $sentence_split,-1,PREG_SPLIT_DELIM_CAPTURE);
   
$newtext = array();
   
$count = sizeof($textbad);
   
    foreach(
$textbad as $key => $string) {
        if (!empty(
$string)) {
           
$text = trim($string, ' ');
           
$size = strlen($text);
           
            if (
$size > 1){    
               
$newtext[] = ucfirst(strtolower($text));
            }
                elseif (
$size == 1) {
                   
$newtext[] = ($text == "\n") ? $text : $text . ' ';
                }     
        }
    }
   
    return
implode($newtext);
}
?>
majd87 at gmail dot com
6.08.2008 3:58
I modified Q1712's code (below) to use regular expressions
instead of characters to properly capitalize words that fall
directly after an unwanted character. See his post for details.

This version allows me to use it around html elements, etc.

<?php
   
function my_ucwords($string){

       
$invalid_characters = array('"',
                                   
'\(',
                                   
'\[',
                                   
'\/',
                                   
'<.*?>',
                                   
'<\/.*?>');

        foreach(
$invalid_characters as $regex){
           
$string = preg_replace('/('.$regex.')/','$1 ',$string);
        }

       
$string=ucwords($string);

        foreach(
$invalid_characters as $regex){
           
$string = preg_replace('/('.$regex.') /','$1',$string);
        }

        return
$string;
    }
?>

Moreover, to get a proper title case, i combine it with this function:

This Function is obtained from:
http://codesnippets.joyent.com/posts/show/716

<?php
   
function title_case($title) {
       
$smallwordsarray = array(
           
'of','a','the','and','an','or','nor','but','is','if','then',
'else','when',
           
'at','from','by','on','off','for','in','out',
'over','to','into','with'
       
);

       
$words = explode(' ', $title);
        foreach (
$words as $key => $word)
        {
            if (
$key == 0 or !in_array($word, $smallwordsarray))
           
$words[$key] = $this->my_ucwords(strtolower($word));
        }

       
$newtitle = implode(' ', $words);
        return
$newtitle;
    }
?>
Hope you find it useful.
gregomm at gmail dot com
23.05.2008 9:38
An improved of ucsentence. In fact its a function to avoid ugly data entry. It's based on code taken from this page. Can be inproved for better performance, of course...

Features:
1- removes duplicated question marks, an exclamations
2- Capitalize first letter of a sentence.
3- split sentences not only with "." but also with "?" and "!"
4- Puts a white space at the begining of each sentence
5- undestand the meaning of "¡" and "¿" in languages like spanish.
6- undestand the htmlentitity version of this simbols.

    function sentenceNormalizer( $sentence_split ) {
       
        $sentence_split = preg_replace("/[!]+/","!",$sentence_split);
        $sentence_split = preg_replace("/[¡]+/","¡",$sentence_split);
        $sentence_split = preg_replace("/[?]+/","?",$sentence_split);
        $sentence_split = preg_replace("/[¿]+/","¿",$sentence_split);       
               
        $textbad = preg_split("/(\<[a-zA-Z0-9-]*\>".
"\!(\s)?|\.(\s)?|\?(\s)?|¿(\s)?|¡(\s)?".
"|&iquest;(\s)?|&iexcl;(\s)?)/",
         $sentence_split,-1,PREG_SPLIT_DELIM_CAPTURE);
        $newtext = array();
        $count = sizeof($textbad);
       
        $prevStr ="";
       
        for ($i = 0; $i < $count; $i++){
           
            $text = trim($textbad[$i]);
            $size = strlen($text);
           
            if ($size>1){           

                $sentencegood=ucfirst(strtolower($text));                 
               
                if ($i>0 && $prevStr != '¿' && $prevStr != '¡' && $prevStr !="&iquest;" && $prevStr !="&iexcl;"){
                    $sentencegood =" ".$sentencegood;
                }
                $newtext[] = $sentencegood;
                $prevStr =$text;
               
            }elseif($size==1 ){
               
                if ($i>0 && ($text == '¿' || $text == '¡' || $prevStr =="&iquest;" || $prevStr =="&iexcl;") ){
                    $newtext[] =" ".$text;                   
                }else{
                    $newtext[] =$text;
                }
                $prevStr =$text;
            }
           
        }
   
        $textgood = implode($newtext);
        return $textgood;
    }
caio dot ariede at gmail dot com
25.03.2008 20:53
To capitalize e-mail address, I made this function.

<?php

   
function capitalize_email($email)
    {
       
$domain = strtolower(strstr($email, '@'));
       
$user = strtolower(substr($email, 0, strrpos($email, '@')));
       
$user = preg_replace('!((?:\b|_).)!e', 'strtoupper("$1");', $user);
        return
$user.$domain;
    }

    echo
capitalize_email('blabla.example@example.com');

   
// Blabla.Example@example.com

?>
NoName
12.03.2008 21:03
For strings with diactrical marks (umlauts, etc.), consider mb_convert_case.
blake at goinoutwest dot com
30.01.2008 2:14
Relating to the mb_ucwords() function posted by Anonymous.  In order for this to actually be multi-byte compliant, you would also need to use mb_substr() and mb_strlen() instead of substr and strlen respectively.

Here it is corrected and extended even further to allow multiple word separators and a list of exceptions to correct after title casing. It's a bit tedious and inelegant, but things frequently are when dealing with human languages.

function mb_ucwords($str) {
    $exceptions = array();
    $exceptions['Hp'] = 'HP';
    $exceptions['Ibm'] = 'IBM';
    $exceptions['Gb'] = 'GB';
    $exceptions['Mb'] = 'MB';
    $exceptions['Cd'] = 'CD';
    $exceptions['Dvd'] = 'DVD';
    $exceptions['Usb'] = 'USB';
    $exceptions['Mm'] = 'mm';
    $exceptions['Cm'] = 'cm';
    //    etc.
   
    $separator = array(" ","-","+");
   
    $str = mb_strtolower(trim($str));
    foreach($separator as $s){
        $word = explode($s, $str);

        $return = "";
        foreach ($word as $val){
            $return .= $s . mb_strtoupper($val{0}) . mb_substr($val,1,mb_strlen($val)-1);
        }
        $str = mb_substr($return, 1);
    }

    foreach($exceptions as $find=>$replace){
        if (mb_strpos($return, $find) !== false){
            $return = str_replace($find, $replace, $return);
        }
    }
    return mb_substr($return, 1);
}
Anonymous
16.12.2007 12:09
Function to do what ucwords is intended to do - just correctly also for international char sets:

function mb_ucwords($s)
{
    $s = mb_strtolower(trim($s));
    $w = explode(" ", $s);
   
    $return = "";
    foreach ($w as $val)
    {
        $return .= " " . mb_strtoupper($val{0}) . substr($val,1,strlen($val)-1);
    }
    return trim($return);
}

Building on an earlier snippet here.
kendsnyder at gmail dot com
30.07.2007 22:02
Here is a function to capitalize a last name, accounting for hyphens, apostrophes, "Mc" and "Mac":

<?php
function CapitalizeLastName($name) {
   
$name = strtolower($name);
   
$name = join("'", array_map('ucwords', explode("'", $name)));
   
$name = join("-", array_map('ucwords', explode("-", $name)));
   
$name = join("Mac", array_map('ucwords', explode("Mac", $name)));
   
$name = join("Mc", array_map('ucwords', explode("Mc", $name)));
    return
$name;
}
?>

I speed tested it against functions that used preg_replace() with an "e" modifier, preg_replace_callback(), and a character-by-character parsing.  Unexpectedly, this function using join(), array_map() and explode() was fastest.
emailfire at gmail dot com
24.05.2007 1:27
To use ucwords with an exception:

<?php
function ucwordss($str, $exceptions) {
$out = "";
foreach (
explode(" ", $str) as $word) {
$out .= (!in_array($word, $exceptions)) ? strtoupper($word{0}) . substr($word, 1) . " " : $word . " ";
}
return
rtrim($out);
}
?>

For example:

<?php
$string
= "my cat is going to the vet";
$ignore = array("is", "to", "the");
echo
ucwordss($string, $ignore);
// My Cat is Going to the Vet
?>
Q1712 at online dot ms
5.05.2007 9:49
ucwords() only excepts whitespace in front of a word, although some chars like '"' or '(' normally have no space between them and the following word:
<?php
$title
= 'ELVIS "THE KING" PRESLEY - (LET ME BE YOUR) TEDDY BEAR';
echo
ucwords(strtolower($title));
?>
prints: Elvis "the King" Presley - (let Me Be Your) Teddy Bear

To avoid this i use a small function adding and deleting blanks behind these chars, and using ucwords() in between:

<?php
function my_ucwords($string)
  {
   
$noletters='"([/'; //add more if u need to
   
for($i=0; $i<strlen($noletters); $i++)
     
$string = str_replace($noletters[$i], $noletters[$i].' ', $string);
   
$string=ucwords($string);
    for(
$i=0; $i<strlen($noletters); $i++)
     
$string = str_replace($noletters[$i].' ', $noletters[$i], $string);
    return
$string;
  }

$title = 'ELVIS "THE KING" PRESLEY - (LET ME BE YOUR) TEDDY BEAR';
echo
my_ucwords(strtolower($title));
?>

prints: Elvis "The King" Presley - (Let Me Be Your) Teddy Bear
chris at cmbuckley dot co dot uk
13.02.2007 18:16
To get some sort of title case with lower-case articles, prepositions etc., try something like this (removing the carriage returns in the regular expression):

<?php
   
function lower_articles($str) {
        return
preg_replace(
           
"/(?<=(?<!:|’s)\W)
            (A|An|And|At|For|In|Of|On|Or|The|To|With)
            (?=\W)/e"
,
           
'strtolower("$1")',
           
$str
       
);
    }
?>

I added the lookbehind (?<!:|’s) because I use this for film titles, where words following those terms should be capitalised (e.g. Lemony Snicket’s A Series of Unfortunate Events, American Pie Presents: The Naked Mile).
barnaby ritchley at exeye dot co dot uk
3.01.2007 17:53
A very easy way to convert to title case:

function titleCase($string)
     {
     return ucwords(strtolower($string));
     }

$myString = "SOME TEXT";

echo titleCase($myString);

//will print, "My Text"
max at phoenixweb dot it
15.09.2006 16:35
I have rewritten a UCSMART function adding a feature to translate special ASCII char (windows occidental ascii charset):

You can edit/add/delete char by use the first two string (be carefull to preserve the order of the string).

Enjoy!

<?
$ASCII_SPC_MIN
= "àáâãäåæçèéêëìíîïðñòóôõöùúûüýÿžš";
$ASCII_SPC_MAX = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝŸŽŠ";

function
str2upper($text) {
    global
$ASCII_SPC_MIN,$ASCII_SPC_MAX;
    return
strtr(strtoupper($text),$ASCII_SPC_MIN,$ASCII_SPC_MAX);
}
function
str2lower($text) {
    global
$ASCII_SPC_MIN,$ASCII_SPC_MAX;
    return
strtr(strtolower($text),$ASCII_SPC_MAX,$ASCII_SPC_MIN);
}
function
ucsmart($text) {
    global
$ASCII_SPC_MIN;
    return
preg_replace(
       
'/([^a-z'.$ASCII_SPC_MIN.']|^)([a-z'.$ASCII_SPC_MIN.'])/e',
       
'"$1".str2upper("$2")',
       
str2lower($text)
    );
}
?>

Massimiliano Cuttini
marco at hotelsandgo dot com
9.09.2006 18:35
ucwords that works also with apices: '
for example: "aquila d'abruzzo" became "Aquila d'Abruzzo"
The second part after the // comment can removed! Is optimized for italian language (leave lowercase articles, prepositions, conjunctions) but easily changeable. Notice che "'s"!! for example "hotel 2000's" becames "Hotel 2000's".

    function my_ucwords($s)
        {
        $a=strtolower($s);
        $s=ucfirst($a);
        for($x=0; $x<strlen($s)-1; $x++)
            if(!ctype_alpha($s[$x])) $s[$x+1]=strtoupper($s[$x+1]);

        //Lascia minuscoli articoli, preposizioni, congiunzioni
        $minuscole=array("il", "lo", "la", "i", "gli", "le",                //ARTICOLI DETERMINATIVI
                 "un", "uno", "una",                        //ARTICOLI INDETERMINATIVI
                 "e",  "d", "l", "s", "un",                    //CONGIUNZIONI e CONTRATTI
                 "di", "a", "da", "in", "con", "su", "per", "tra", "fra",    //PREPOSIZIONI SEMPLICI
                 "del", "dello", "della", "dei", "degli", "delle",        //PREPOSIZIONI ARTICOLATE
                 "a", "al", "allo", "alla", "ai", "agli", "alle",
                 "da", "dal", "dallo", "dalla", "dai", "dagli", "dalle",
                 "in", "nel", "nello", "nella", "nei", "negli", "nelle",
                 "con", "col", "collo", "colla", "coi", "cogli", "colle",
                 "su", "sul", "sullo", "sulla", "sui", "sugli", "sulle",
                 "per", "pel", "pei");
       
        foreach($minuscole as $value)
            {
            $pos=strpos($a, $value);
            if( ( $pos>0 && $pos<strlen($s)-1 && !ctype_alpha($a[$pos-1]) && !ctype_alpha($a[$pos+1]) )    //CARATTERE IN MEZZO
            ||  ( $pos==strlen($s)-1 && !ctype_alpha($a[$pos-1]) ) )                    //CASO PARTICOLARE: carattere in fondo. x es: "hotel 2000's"
                $s[$pos]=strtolower($s[$pos]);
            }

        return $s;
        }

    function my_ucwords_essential($s)
        {
        $a=strtolower($s);
        $s=ucfirst($a);
        for($x=0; $x<strlen($s)-1; $x++)
            if(!ctype_alpha($s[$x])) $s[$x+1]=strtoupper($s[$x+1]);

        return $s;
        }

Can be rewritten better, I Know
philip at fcknet dot dk
8.08.2006 14:11
Before noticing this function I made the following function (that does the same as ucwords):

<?php
function firstUpper($string)
{
   
$string = str_replace(array("Æ","Ø","Å"), array("æ","ø","å"), strtolower($string));
   
$ord = explode(" ", $string);
   
   
$return = "";
    foreach (
$ord as $val)
    {

       
$return .= " " . str_replace(array("æ","ø","å"), array("Æ","Ø","Å"), strtoupper($val{0})) . substr($val,1,strlen($val)-1);
    }
    return
$return;
}
?>

It also converts Danish letters without using the setlocale function.
starmonkey [at] evolove [dot] net
5.08.2006 17:17
Simple helper function to walk through a nested array of strings and upper case them:

<?php
/**
 * Helper function to convert an array of strings to upper case words
 */
function _capitalize($input) {
    if(
is_array($input)) {
       
// recurse through array elements (using a reference)
       
foreach($input as &$value) {
           
$value = _capitalize($value);
        }
        return
$input;
    } elseif(
is_string($input)) {
       
// process this string
       
return ucwords($input);
    } else {
       
// all other data types, leave alone
       
return $input;
    }
}
?>
Ismet Togay
31.05.2006 2:07
Response to arif:

We do not need that long functions. In order to make ucwords() worked properly in Turkish words that contain speacial characters, we can use the following command in our php codes:

setlocale(LC_ALL, 'tr_TR');

This will set locale to Turkish.
lev at phpfox dot com
6.05.2006 10:44
In the function ucsmart() posted by ieure at php dot net on 04-Dec-2005 11:57, I found a similar problem in this function to what he found in igua's.

<?php
function ucsmart($text)
{
   return
preg_replace('/([^a-z]|^)([a-z])/e', '"$1".strtoupper("$2")',
                      
strtolower($text));
}
?>

"igua's code adds a backslash in front of the first single quote for me. This doesn't alter the content in any way other than changing case."

Actually, it did end up changing the content for me (php 5.0.4) in the way that this function escapes a single quotation (apostrophe) in the MIDDLE of a word.

For example:

who's online?

Became:

Who\'s Online?

The fix is simple however, and merely requires fine-tuning the regular expression:

<?php
function ucsmart($text)
{
   return
preg_replace('/([^a-z\']|^)([a-z])/e', '"$1".strtoupper("$2")',
                      
strtolower($text));
}
?>

(note: while previewing this note before adding it, I am noticing php's website is not correctly displaying the change I made as I wrote it. After the first a-z in the expression, the single quotation should be escaped... If it isn't you will get a parse error! And apoligies if my text here is colored as php code; not my fault!)

This will not escape a single quotation mark which occurs in the middle of a word... Though, you may find that might need to add other characters inside the regular expression if you use other special characters inside your words and if you get funky output.

It's a great expression though! Simple, yet very powerful. Kudos!
arif
29.03.2006 10:12
it can be used for Turkish alphabet.

function strtoupperTR($str){
   return strtr($str,
   "abcçdefgğhıijklmnoöpqrsştuüvwxyz",
   "ABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ");
}
function strtolowerTR($str){
   return strtr($str,
   "ABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ",
   "abcçdefgğhıijklmnoöpqrsştuüvwxyz");
}

function ucwordsTR($str)
{
   return preg_replace('/([^a-zığüşçö]|^)([a-zığüşçö])/e', '"$1".strtoupperTR("$2")',
                       strtolowerTR($str));
}

24.12.2005 16:34
"ieure at php dot net", your idea is pure poetry!

The function below will standardize the capitalization on people's names and the titles of reports and essays . You may need to adapt the lists in "$all_uppercase" and "$all_lowercase" to suit the data that you are working with.

function my_ucwords($str, $is_name=false) {
   // exceptions to standard case conversion
   if ($is_name) {
       $all_uppercase = '';
       $all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And';
   } else {
       // addresses, essay titles ... and anything else
       $all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';
       $all_lowercase = 'A|And|As|By|In|Of|Or|To';
   }
   $prefixes = 'Mc';
   $suffixes = "'S";

   // captialize all first letters
   $str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($str)));

   if ($all_uppercase) {
       // capitalize acronymns and initialisms e.g. PHP
       $str = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $str);
   }
   if ($all_lowercase) {
       // decapitalize short words e.g. and
       if ($is_name) {
           // all occurences will be changed to lowercase
           $str = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $str);
       } else {
           // first and last word will not be changed to lower case (i.e. titles)
           $str = preg_replace("/(?<=\\W)($all_lowercase)(?=\\W)/e", 'strtolower("$1")', $str);
       }
   }
   if ($prefixes) {
       // capitalize letter after certain name prefixes e.g 'Mc'
       $str = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $str);
   }
   if ($suffixes) {
       // decapitalize certain word suffixes e.g. 's
       $str = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $str);
   }
   return $str;
}

// A name example
print my_ucwords("MARIE-LOU VAN DER PLANCK-ST.JOHN", true);
// Output: Marie-Lou van der Planc-St.John

// A title example
print my_ucwords("to be or not to be");
// Output: "To Be or Not to Be"
ieure at php dot net
4.12.2005 23:57
Whoa guys, tone things down a bit here. No need to loop and implode. This is a one-line solution:

function ucsmart($text)
{
    return preg_replace('/([^a-z]|^)([a-z])/e', '"$1".strtoupper("$2")',
                        strtolower($text));
}

igua's code adds a backslash in front of the first single quote for me. This doesn't alter the content in any way other than changing case.
gothicbunny at hotmail dot com
9.11.2005 1:16
Here is a simple, yet winded, opposite to ucwords.

<?php
/*
    # lcwords v1.000
    # Convert the first word character to lowercase (opposite to ucwords)
    # input string
    # return string
*/
function lcwords($string)
{
   
/* Some temporary variables */
    #loop variable
   
$a = 0;
   
#store all words in this array to be imploded and returned
   
$string_new = array();
   
#create array of all words
   
$string_exp = explode(" ",$string);
    foreach(
$string_exp as $astring)
    {
        for(
$a=0;$a<strlen($astring);$a++)
        {
           
#check that the character we are at {pos $a} is a word
            #i.e. if the word was !A the code would fail at !
            #then loop to the next character and succeed at A
            #check at character position $a
           
if(preg_match("'\w'",$astring[$a]))
            {
               
$astring[$a] = strtolower($astring[$a]);
               
#end the loop
               
break;
            }
        }
       
$string_new[] = $astring;
    }
   
#recreate the string from array components using space deliminator
   
return implode(" ",$string_new);
}
?>

Of course a simplier way would be to use a callback, but I like working with long code :)

21.10.2005 1:14
Here's a  piece that allows you to use the contents of a directory..  capitalizes the words and make links.. this particular example splits file names at _ and only selects file with .htm extensions (thought you could use any extension and call it using  include()  or soom such)
ie my_file_name.htm will produce
<a href="my_file_name.htm">My File Name</a>

<?php
$path
= "/home/path/to/your/directory";
  
$mydir = dir($path);
   while((
$file = $mydir->read()) !== false) {
     if(
substr($file, -4)=='.htm'){
   
$trans = array("_" => " ", ".htm" => ""); // creates the editing array
   
$newlist = strtr($file, $trans); // edits using editing array
   
echo "<a href=\"".$file."\">".ucwords($newlist)."</a><br>";
     }
    }
  
?>
Static Bit
18.09.2005 17:01
// programming/repair -> Programming/Repair
// mcdonald    o'neil   -> McDonand O'Neil
// art    of street        -> Art of Street

function NomeProprio($nome)
   {
   //two space to one
   $nome = str_replace("  ", " ", $nome);
   $nome = str_replace("  ", " ", $nome);
   $nome = str_replace("  ", " ", $nome);

   $intervalo = 1;
   for ($i=0; $i < strlen($nome); $i++)
       {
       $letra = substr($nome,$i,1);
       if (((ord($letra) > 64) && (ord($letra) < 123)) || ((ord($letra) > 48) && (ord($letra) < 58)))
          {
          $checa_palavra = substr($nome, $i - 2, 2);
          if (!strcasecmp($checa_palavra, 'Mc') || !strcasecmp($checa_palavra, "O'"))
             {
             $novonome .= strtoupper($letra);
             }
            elseif ($intervalo)
             {
             $novonome .= strtoupper($letra);
             }
            else
             {
             $novonome .= strtolower($letra);
             }
          $intervalo=0;
          }
         else
          {
          $novonome .= $letra;
          $intervalo = 1;
          }
       }
   $novonome = str_replace(" Of ", " of ", $novonome);
   $novonome = str_replace(" Da ", " da ", $novonome);
   $novonome = str_replace(" De ", " de ", $novonome);
   $novonome = str_replace(" Do ", " do ", $novonome);
   $novonome = str_replace(" E " , " e " , $novonome);
   return $novonome;
   }
radley25 at nospam dot spamcop dot net
5.07.2005 5:06
In response to joshuamallory at yahoo dot com:

Using CSS to fix a PHP fault is not the ideal way to solve a problem. CSS is browser dependent and can only be used when the data is presented in a web page. A better fix would be something like this:

<?php
function better_ucwords($string) {
  
$string = ucwords($string);
  
$string = preg_replace('#[\\/][a-z]#e', "strtoupper('$0')", $string);
   return
$string;
}
?>
igua no-spam at coveruniverse dot com
8.03.2005 13:30
The code posted by neil doesn't fully do what is wanted. Try adding some more question marks at the end and it will return a not wanted string.

Below code will uppercase all your words regardless of the delimiter.

<?php
$text
= "What?No 'delimiters',shit \"happens\" here.this solves all problems???";
preg_match_all('/[A-Za-z]+|[^A-Za-z]+/', $text, $data);
for (
$i = 0; $i < count($data[0]); $i++) {
 
$data[0][$i] = ucfirst($data[0][$i]);
}
$text = implode("", $data[0]);
print
$text;
?>
arjini at gmail dot com
23.01.2005 21:20
Not so much ucwords() related as it is capital letter related. I often use camel casing (as do wikis), I needed a reason to reverse the camel casing.

function unCamelCase($str){
    $bits = preg_split('/([A-Z])/',$str,false,PREG_SPLIT_DELIM_CAPTURE);
    $a = array();
    array_shift($bits);
    for($i = 0; $i < count($bits); ++$i)
        if($i%2)
            $a[] = $bits[$i - 1].$bits[$i];
    return $a;
}

print_r(unCamelCase('MyFancyCamelCasedWord'));

Array
(
    [0] => My
    [1] => Fancy
    [2] => Camel
    [3] => Cased
    [4] => Word
)
joshuamallory at yahoo dot com
15.11.2004 5:08
If you want to format a string like...

<?php
    $string
= "computer programming/repair";
    print
ucwords($string);
?>

Output: Computer Programming/repair

Notice the word after the slash (Programming/repair) isn't capitalized. To fix this, use CSS...

<?php
    $string
= "computer programming/repair";
    print
'<p style="text-transform:capitalize">';
    print
ucwords($string);
    print
'<p>';
?>
babel - nospamplease - sympatico - ca
11.02.2004 5:26
Correction to the code of firewire at itsyourdomain dot com:

preg_replace_callback('/\b(\w)(\w+)?/',
   create_function('$a',
   'return strtoupper($a[1]) . ((sizeof($a) > 2 ) ? 
       strtolower($a[2]) : "");'),
    'p.s.: hello.this is my string.');

Will work with punctuation as well as spaces.
deepdene at email dot com
10.12.2002 8:20
A function knowing about name case (i.e. caps on McDonald etc)

function name_case($name)
{
    $newname = strtoupper($name[0]);   
    for ($i=1; $i < strlen($name); $i++)
    {
        $subed = substr($name, $i, 1);    
        if (((ord($subed) > 64) && (ord($subed) < 123)) ||
            ((ord($subed) > 48) && (ord($subed) < 58)))
        {
            $word_check = substr($name, $i - 2, 2);
            if (!strcasecmp($word_check, 'Mc') || !strcasecmp($word_check, "O'"))
            {
                $newname .= strtoupper($subed); 
            }
            else if ($break)
            {
               
                $newname .= strtoupper($subed);
            }
            else      
            {
                $newname .= strtolower($subed);
            }
             $break=0;
        }
        else
        {
            // not a letter - a boundary
             $newname .= $subed;
            $break=1;
        }
    }   
    return $newname;
}
firewire at itsyourdomain dot com
20.11.2002 0:13
For those that want to capitalize based on a regular expression.
print preg_replace_callback('/(\s|^)[a-z]/', create_function('$a', 'return strtoupper($a[0]);'), 'hello this is my string');

This is a quick untested example.
anton at titov dot net
25.09.2002 19:56
for those, who not avoid regular expressions, solution of discussed problem:

$text=preg_replace('/(\W)(\w)/e', '"\\1".strtoupper("\\2")', ucfirst(strtolower($text)));
fille at fukt dot bth dot se
27.08.2002 17:04
[Editor's note: For details on the bug see
http://bugs.php.net/bug.php?id=14655]

This function has a bug, and while waiting for the bug fix, here is a work-around pice of code.

When using international letters, you will get into troubles with the ucwords() function.

Example:

$string="xxxxx" will be "XxxXxx" after beeing processed by ucwords().

To get around it, I wrote some extra code that checks the string once more, and lowercases all letters that is not in the beginning of a word.

$string=ucwords($string);
//Bugfix from here on
for($i=0;$i<strlen($string);$i++)
    if((ctype_upper($string[$i]) &&( $string[$i-1]==" " || $i==0 ))!=TRUE)
        $string[$i]=strtolower($string[$i]);

Thia code is also an optional way of doing the same work on a string that is totally UPPERCASE.

27.08.2002 16:20
Beware of language when using this function to collate personal names! This may not work with some languages and this depends on the current locale!
So it's best to simply use strtoupper() or strtolower(strtoupper()) to collate names for searches in a database. Avoid using strtolower() directly, as it won't collate some characters like the german '' into 'ss'.
Capitalizing names is very language dependant: don't do it on address fields such as city names. Prefer uppercasing, or keep the original case if the string must be displayed to a user!

19.01.2002 3:14
This seems to be what people want:

function uc_all($string) {
    $temp = preg_split('/(\W)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
    foreach ($temp as $key=>$word) {
        $temp[$key] = ucfirst(strtolower($word));
    }
    return join ('', $temp);
}

[ed note: fixed the code to be correct]
Julienc at psychologie-fr dot com
4.11.2001 4:06
Its still possible to clean a bit more the previous sample:

$string=strtolower($string); $break=true;
for ($i=0; $i < strlen($string); $i++) { $subed=$string[$i];
if (((ord($subed) > 64) && (ord($subed) < 123)) || ((ord($subed) > 48) && (ord($subed) < 58))) {
if ($break) { $string[$i] = strtoupper($subed); }
$break=false; } else { $break=true; }
}

- Julien
mistcat at mistcat dot com
29.03.2001 0:00
Actually that code would work if you changed this line:
$words[0][] = $lastword;
to
$words[0][] = $lastword[0];
neil at no-spam-ents24 dot com
21.03.2001 14:10
The code posted above by Joerg Krause only works for a string which ends with one of the delimiters. A possible fix is:

<?php
$text
= "What?No delimiters,shit happens here.this solves all problems.";
preg_match_all("/(\w+[,. ?])+/U", $text, $words);
preg_match("/(\w+)$/", $text, $lastword);
$words[0][] = $lastword;
foreach(
$words[0] as $part) $uwords[] = ucfirst($part);
$text = implode("", $uwords);
echo
$text;
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)