dave-hunt
1/3/2018 - 9:32 PM

Helper - Convert a String to Title Case (first letter of each word uppercased except small words)

Helper - Convert a String to Title Case (first letter of each word uppercased except small words)

<?php 
function strtotitlecase( $title ) { 
	$smallwords = array( 'the','a','to','and','an','or','nor','but','of','is','if','then','else','at','from','when','by','on','off','for','in','out','with','over','into' ); 
	$words = explode( ' ', $title ); 
	foreach ( $words as $key => $word ) { 
		if ( $key == 0 or !in_array( $word, $smallwords ) ) {
			$words[ $key ] = ucwords( $word ); 
		}
	}
	$newtitle = implode( ' ', $words ); 
	return $newtitle; 
}

echo strtotitlecase("this is a title of an article"); // This is a Title of an Article
?>