ucwords() to convert a string to title case in PHP

I enjoy the challenge of solving a problem as much as the next guy, but sometimes it seems that PHP programmers have a tendency to apply great ingenuity in creating complex solutions for a fairly simple problem that actually already has a very simple solution. What’s more, these bits of code (we’ll call them “better mouse traps”) usually end up posted on a blog someplace to be picked up and spread into countless people’s applications.

have a look at this function I stumbled upon today…

function  titleCase($string)  { 
        $len=strlen($string); 
        $i=0; 
        $last= ""; 
        $new= ""; 
        $string=strtoupper($string); 
        while  ($i<$len): 
                $char=substr($string,$i,1); 
                if  (ereg( "[A-Z]",$last)): 
                        $new.=strtolower($char); 
                else: 
                        $new.=strtoupper($char); 
                endif; 
                $last=$char; 
                $i++; 
        endwhile; 
        return($new); 
};

That's a function that takes a string, retrieves it's length, converts it to upper case, then loops through the characters in the string to detect word breaks and then convert to lower case individually each character that isn't the start of a word. The author proposed this as a function to capitalizing strings for titles. It's a pretty solid little function which displays a good bit of ingenuity in the understanding and use of some slightly more advanced aspects of PHP. Hell, it's actually pretty slick! And I found this same function posted on several websites.

The thing is...

echo ucwords('there is a PHP native function to upper case words in a string!');

...and that function is ucwords()

4 Comments

  1. JimG

    # December 24, 2008 - 10:29 pm

    Where the above complex script excels is in cleaning up messy user input—particularly in fixing titles set in all caps. The ucwords function will not alter characters already set in uppercase whereas the one above will. While you could initially overcome this by applying strtolower($string) then ucwords($string), you run into problems with words immediately following certain punctuation (such as slashes, brackets, parens, etc.). That said, there may be instances where simple strings will be perfectly served by ucwords.

  2. Jim Mayes

    # December 25, 2008 - 1:06 am

    Thanks for pointing that out Jim! I hadn’t considered the need to address other characters like parentheses, slashes etc and how they affect capitalization within a string, which really reinforces just how slick of a function you have there. As you mention, even the combination of ucwords() with strlower() would fail to catch those instances. Reconsidering the nature of the problem, mb_convert_case() may be worth a look.

    $str = 'This is a Title (WITH PARENTHESES!)';
    echo titleCase($str);
    echo mb_convert_case($str, MB_CASE_TITLE);
    

    Of course I haven’t done extensive testing of the two head to head (just the test above), and I should have done more testing before I pulled the trigger and wrote the original post suggesting ucwords(). And that’s also with the caveat that you will need multibyte string support compiled into PHP to have the mb_convert_case() function available.

  3. Jim Mayes

    # December 25, 2008 - 1:16 am

    And of course, none of these solutions address the fact that stylistically speaking, it may not be appropriate to capitalize certain words in a title (prepositions, conjunctions, articles). For example the ‘is’ and ‘a’ in the above code.

  4. Jonathan

    # March 2, 2011 - 5:24 pm

    not to mention hyphenated names and initials.

Leave a Reply