Grammatically Correct PHP Title Capitalization Function

Sometimes a developer has to get their titles from crazy sources that aren’t always capitalized correctly. Nobody wants to change all these titles every time they show up to make the right words capitalized, so here’s a little PHP function that corrects them all for you:


function ucTitle($title){
$title = ucwords($title);
$title = str_replace(' A ', ' a ', $title);
$title = str_replace(' And ', ' and ', $title);
$title = str_replace(' Any ', ' any ', $title);
$title = str_replace(' As ', ' as ', $title);
$title = str_replace(' At ', ' at ', $title);
$title = str_replace(' Aboard ', ' Aboard ', $title);
$title = str_replace(' About ', ' About ', $title);
$title = str_replace(' Above ', ' Above ', $title);
$title = str_replace(' Across ', ' Across ', $title);
$title = str_replace(' After ', ' After ', $title);
$title = str_replace(' Against ', ' Against ', $title);
$title = str_replace(' Along ', ' Along ', $title);
$title = str_replace(' Amid ', ' Amid ', $title);
$title = str_replace(' Among ', ' Among ', $title);
$title = str_replace(' Anti ', ' Anti ', $title);
$title = str_replace(' Around ', ' Around ', $title);

$title = str_replace(' But ', ' but ', $title);
$title = str_replace(' By ', ' by ', $title);
$title = str_replace(' Before ', ' before ', $title);
$title = str_replace(' Behind ', ' behind ', $title);
$title = str_replace(' Below ', ' below ', $title);
$title = str_replace(' Beneath ', ' beneath ', $title);
$title = str_replace(' Beside ', ' beside ', $title);
$title = str_replace(' Besides ', ' besides ', $title);
$title = str_replace(' Between ', ' between ', $title);
$title = str_replace(' Beyond ', ' beyond ', $title);

$title = str_replace(' Concerning ', ' concerning ', $title);
$title = str_replace(' Considering ', ' considering ', $title);

$title = str_replace(' Despite ', ' despite ', $title);
$title = str_replace(' Down ', ' down ', $title);
$title = str_replace(' During ', ' during ', $title);

$title = str_replace(' except ', ' except ', $title);
$title = str_replace(' excepting ', ' excepting ', $title);
$title = str_replace(' excluding ', ' excluding ', $title);

$title = str_replace(' For ', ' for ', $title);
$title = str_replace(' Following ', ' following ', $title);
$title = str_replace(' From ', ' from ', $title);

$title = str_replace(' If ', ' if ', $title);
$title = str_replace(' In ', ' in ', $title);
$title = str_replace(' Inside ', ' inside ', $title);
$title = str_replace(' Into ', ' into ', $title);

$title = str_replace(' Like ', ' like ', $title);

$title = str_replace(' Minus ', ' minus ', $title);

$title = str_replace(' Nor ', ' nor ', $title);
$title = str_replace(' Near ', ' near ', $title);

$title = str_replace(' Of ', ' of ', $title);
$title = str_replace(' On ', ' on ', $title);
$title = str_replace(' Or ', ' or ', $title);
$title = str_replace(' Off ', ' off ', $title);
$title = str_replace(' Onto ', ' onto ', $title);
$title = str_replace(' Opposite ', ' opposite ', $title);
$title = str_replace(' Outside ', ' outside ', $title);
$title = str_replace(' Over ', ' over ', $title);

$title = str_replace(' Past ', ' past ', $title);
$title = str_replace(' Per ', ' per ', $title);
$title = str_replace(' Plus ', ' plus ', $title);

$title = str_replace(' Regarding ', ' regarding ', $title);
$title = str_replace(' Round ', ' round ', $title);

$title = str_replace(' So ', ' so ', $title);
$title = str_replace(' Save ', ' save ', $title);
$title = str_replace(' Since ', ' since ', $title);

$title = str_replace(' Than ', ' than ', $title);
$title = str_replace(' The ', ' the ', $title);
$title = str_replace(' Through ', ' through ', $title);
$title = str_replace(' To ', ' to ', $title);
$title = str_replace(' Toward ', ' toward ', $title);
$title = str_replace(' Towards ', ' towards ', $title);

$title = str_replace(' Under ', ' under ', $title);
$title = str_replace(' Underneath ', ' underneath ', $title);
$title = str_replace(' Unlike ', ' unlike ', $title);
$title = str_replace(' Until ', ' until ', $title);
$title = str_replace(' Up ', ' up ', $title);
$title = str_replace(' Upon ', ' upon ', $title);

$title = str_replace(' Versus ', ' versus ', $title);
$title = str_replace(' Via ', ' via ', $title);

$title = str_replace(' With ', ' with ', $title);
$title = str_replace(' Within ', ' within ', $title);
$title = str_replace(' Without ', ' without ', $title);

$string = $title;
$my_array = explode(" ", $string);
$my_count = count($my_array);
$my_last = $my_count -1;
$last_word = ucfirst($my_array[$my_last]);
$my_array[$my_last] = $last_word;
$i=0;
$new_string = "";
while($i < $my_count){
$new_string = $new_string." ".$my_array[$i];
$i++;
}
$title = ucfirst($new_string);
return ($title);
}
//provided by: www.codebangers.com

There it is, stick this in your functions.php file and give it the “title” text you want to appear correctly, rest easy knowing your titles are pro.

For those of you who have never used PHP functions, you put the above code in a “functions.php” file and include it like this in your PHP:


include_once('htdocs/functions.php');

Next, for this particular function, you would call to it like so:


ucTitle($title);

With “$title” being what is getting corrected.

Leave a Reply