Title Case in PHP

    Thomas Rutter
    Thomas Rutter
    Share

    The PHP functions strtoupper and ucwords capitalise all characters in a string, and the first letter of every word in a string, respectively. However, there exists in the standard PHP library no way of achieving Title Case, which involves capitalising all words except for small words (such as conjunctions) when they are not the first word.

    The following mini-tutorial will provide a solution similar to the one I came up with for displaying thread titles in the SitePoint Forums’ Highlighted Forum Discussions.

    First, we’ll need a list of all the words which we don’t want to capitalise when they are not the first word. The words we should be concerned about are conjunctions (such as and, or), prepositions (in, on) and internal articles (the, a). However, I’m no expert in English grammar so I’ll just call them ‘small words’. Here’s a selection of the ones I use.

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

    The explode function in PHP can be used to split any string into an array of strings, using a character as a split character. So if we split with a space character (‘ ‘), we can use explode to split our string into words.

    $words = explode(' ', 'this is a title');

    $words becomes an array of strings, each string representing one word from the original $title. Here it would be equal to array(‘This’, ‘is’, ‘a’, ‘title’).

    We can now operate on each word separately. We need to test each word to determine if it is one of our ‘small words’. If it is not a small word, or it’s the first word, it should be capitalised. To operate on each member of an array in turn, we can use the PHP language construct foreach. To check if a word is one of our small words, we can use in_array

    foreach ($words as $key => $word) { if (!$key or !in_array($word, $smallwordsarray)) $words[$key] = ucwords($word); }

    Notice here that I assigned the value to $words[$key] and not to $word. The reason for this is that $word was created by the foreach statement. Modifying $word will not cause any change to the original array $words. So I need to modify the entry in the original array $words that corresponds to the current array key.

    By this point, we have an array of all the words in our title. Those words which should be capitalised have been, and all that’s left is to join the words together again into a single string. This can be done with implode, which works in the opposite direction to explode.

    $newtitle = implode(' ', $words);

    Here’s the entire script as a function you’re welcome to use in your application.

    function strtotitle($title) // Converts $title to Title Case, and returns the result. { // Our array of 'small words' which shouldn't be capitalised if // they aren't the first word. Add your own words to taste. $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' ); // Split the string into separate words $words = explode(' ', $title); foreach ($words as $key => $word) { // If this word is the first, or it's not one of our small words, capitalise it // with ucwords(). if ($key == 0 or !in_array($word, $smallwordsarray)) $words[$key] = ucwords($word); } // Join the words back into a string $newtitle = implode(' ', $words); return $newtitle; }

    Notice that if the input already contains capital letters, those letters will remain. This ensures that letters that must always be capital, such as acronyms, will remain so.

    Try it out with a title, such as

    echo strtotitle("this is a title");

    The result? “This is a Title”.

    As an aside, a quick search on Google found a solution to the same problem in JavaScript. However, it is a nightmare of code – unnecessarily complex. It checks every letter separately! You’d be better off porting my script to JavaScript.

    By the way, here’s a similar solution in ColdFusion.

    Frequently Asked Questions (FAQs) about Title Case in PHP

    What is the difference between ucwords() and ucfirst() in PHP?

    In PHP, both ucwords() and ucfirst() are built-in functions used to convert the first letter of a string to uppercase. However, they differ in their scope of operation. The ucwords() function converts the first letter of each word in a string to uppercase, while ucfirst() only converts the first letter of the first word in a string. For instance, if you have a string “hello world”, ucwords() will return “Hello World” while ucfirst() will return “Hello world”.

    How can I convert a string to title case in PHP?

    PHP does not have a built-in function to convert a string to title case. However, you can achieve this by combining the ucwords() function with strtolower(). First, convert the entire string to lowercase using strtolower(), then use ucwords() to capitalize the first letter of each word. Here’s an example:

    $string = "HELLO WORLD";
    $title_case = ucwords(strtolower($string));
    echo $title_case; // Outputs: Hello World

    Can I use ucwords() function with multi-byte string in PHP?

    Yes, PHP provides a multi-byte version of ucwords() function called mb_convert_case(). This function is useful when dealing with multi-byte characters like those found in UTF-8 strings. It takes two arguments: the string to be converted and the mode of conversion. To convert to title case, use MB_CASE_TITLE as the mode.

    $string = "こんにちは 世界";
    $title_case = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
    echo $title_case; // Outputs: こんにちは 世界

    How can I exclude certain words from being capitalized when using ucwords() in PHP?

    PHP’s ucwords() function does not provide an option to exclude certain words from being capitalized. However, you can achieve this by writing a custom function. This function would split the string into words, capitalize each word that is not in the exclusion list, and then join the words back together.

    Why does ucwords() not work with hyphenated words in PHP?

    The ucwords() function in PHP considers a “word” as any sequence of characters separated by whitespace. Therefore, it does not recognize hyphenated words as separate words and does not capitalize the first letter after a hyphen. To overcome this, you can use a custom function that replaces hyphens with spaces, applies ucwords(), and then replaces spaces back with hyphens.

    Can I use ucwords() function with arrays in PHP?

    The ucwords() function in PHP only works with strings. If you want to apply ucwords() to each element of an array, you can use the array_map() function. This function applies a specified callback function to each element of an array.

    How can I make the first letter of each word uppercase in PHP, but keep the rest of the word in lowercase?

    You can achieve this by combining the ucwords() function with strtolower(). First, convert the entire string to lowercase using strtolower(), then use ucwords() to capitalize the first letter of each word.

    Is there a way to use ucwords() function in PHP to capitalize the first letter after a punctuation mark?

    The ucwords() function in PHP does not recognize punctuation marks as word separators. Therefore, it does not capitalize the first letter after a punctuation mark. To achieve this, you can write a custom function that replaces punctuation marks with spaces, applies ucwords(), and then replaces spaces back with punctuation marks.

    How can I convert a string to sentence case in PHP?

    PHP does not have a built-in function to convert a string to sentence case. However, you can achieve this by using the ucfirst() function. This function only capitalizes the first letter of the string, effectively converting it to sentence case.

    Can I use ucwords() function with numbers in PHP?

    Yes, you can use the ucwords() function with numbers in PHP. However, since numbers do not have an uppercase or lowercase, the function will have no effect on them. It will only capitalize the first letter of each word in the string.