Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Make Title Case (See related posts)

// Run text throughthis to make it title case
// "from russia with love" becomes:
// "From Russia with love"


function title_case($title) {
  // 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(strtolower($word));
  }
  // Join the words back into a string
  $newtitle = implode(' ', $words);
  return $newtitle;
}


You need to create an account or log in to post comments to this site.


Related Posts