{"id":299,"date":"2012-03-25T16:03:49","date_gmt":"2012-03-25T16:03:49","guid":{"rendered":"http:\/\/codebangers.com\/?p=299"},"modified":"2012-03-25T16:06:10","modified_gmt":"2012-03-25T16:06:10","slug":"grammatically-correct-php-title-capitalization-function","status":"publish","type":"post","link":"https:\/\/codebangers.com\/grammatically-correct-php-title-capitalization-function\/","title":{"rendered":"Grammatically Correct PHP Title Capitalization Function"},"content":{"rendered":"<p>Sometimes a developer has to get their titles from crazy sources that aren&#8217;t always\u00a0capitalized correctly. Nobody wants to change all these titles every time they show up to make the right words capitalized, so here&#8217;s a little PHP function that corrects them all for you:<\/p>\n<p><code><br \/>\nfunction ucTitle($title){<br \/>\n$title = ucwords($title);<br \/>\n$title = str_replace(' A ', ' a ', $title);<br \/>\n$title = str_replace(' And ', ' and ', $title);<br \/>\n$title = str_replace(' Any ', ' any ', $title);<br \/>\n$title = str_replace(' As ', ' as ', $title);<br \/>\n$title = str_replace(' At ', ' at ', $title);<br \/>\n$title = str_replace(' Aboard ', ' Aboard ', $title);<br \/>\n$title = str_replace(' About ', ' About ', $title);<br \/>\n$title = str_replace(' Above ', ' Above ', $title);<br \/>\n$title = str_replace(' Across ', ' Across ', $title);<br \/>\n$title = str_replace(' After ', ' After ', $title);<br \/>\n$title = str_replace(' Against ', ' Against ', $title);<br \/>\n$title = str_replace(' Along ', ' Along ', $title);<br \/>\n$title = str_replace(' Amid ', ' Amid ', $title);<br \/>\n$title = str_replace(' Among ', ' Among ', $title);<br \/>\n$title = str_replace(' Anti ', ' Anti ', $title);<br \/>\n$title = str_replace(' Around ', ' Around ', $title);<\/p>\n<p>$title = str_replace(' But ', ' but ', $title);<br \/>\n$title = str_replace(' By ', ' by ', $title);<br \/>\n$title = str_replace(' Before ', ' before ', $title);<br \/>\n$title = str_replace(' Behind ', ' behind ', $title);<br \/>\n$title = str_replace(' Below ', ' below ', $title);<br \/>\n$title = str_replace(' Beneath ', ' beneath ', $title);<br \/>\n$title = str_replace(' Beside ', ' beside ', $title);<br \/>\n$title = str_replace(' Besides ', ' besides ', $title);<br \/>\n$title = str_replace(' Between ', ' between ', $title);<br \/>\n$title = str_replace(' Beyond ', ' beyond ', $title);<\/p>\n<p>$title = str_replace(' Concerning ', ' concerning ', $title);<br \/>\n$title = str_replace(' Considering ', ' considering ', $title);<\/p>\n<p>$title = str_replace(' Despite ', ' despite ', $title);<br \/>\n$title = str_replace(' Down ', ' down ', $title);<br \/>\n$title = str_replace(' During ', ' during ', $title);<\/p>\n<p>$title = str_replace(' except ', ' except ', $title);<br \/>\n$title = str_replace(' excepting ', ' excepting ', $title);<br \/>\n$title = str_replace(' excluding ', ' excluding ', $title);<\/p>\n<p>$title = str_replace(' For ', ' for ', $title);<br \/>\n$title = str_replace(' Following ', ' following ', $title);<br \/>\n$title = str_replace(' From ', ' from ', $title);<\/p>\n<p>$title = str_replace(' If ', ' if ', $title);<br \/>\n$title = str_replace(' In ', ' in ', $title);<br \/>\n$title = str_replace(' Inside ', ' inside ', $title);<br \/>\n$title = str_replace(' Into ', ' into ', $title);<\/p>\n<p>$title = str_replace(' Like ', ' like ', $title);<\/p>\n<p>$title = str_replace(' Minus ', ' minus ', $title);<\/p>\n<p>$title = str_replace(' Nor ', ' nor ', $title);<br \/>\n$title = str_replace(' Near ', ' near ', $title);<\/p>\n<p>$title = str_replace(' Of ', ' of ', $title);<br \/>\n$title = str_replace(' On ', ' on ', $title);<br \/>\n$title = str_replace(' Or ', ' or ', $title);<br \/>\n$title = str_replace(' Off ', ' off ', $title);<br \/>\n$title = str_replace(' Onto ', ' onto ', $title);<br \/>\n$title = str_replace(' Opposite ', ' opposite ', $title);<br \/>\n$title = str_replace(' Outside ', ' outside ', $title);<br \/>\n$title = str_replace(' Over ', ' over ', $title);<\/p>\n<p>$title = str_replace(' Past ', ' past ', $title);<br \/>\n$title = str_replace(' Per ', ' per ', $title);<br \/>\n$title = str_replace(' Plus ', ' plus ', $title);<\/p>\n<p>$title = str_replace(' Regarding ', ' regarding ', $title);<br \/>\n$title = str_replace(' Round ', ' round ', $title);<\/p>\n<p>$title = str_replace(' So ', ' so ', $title);<br \/>\n$title = str_replace(' Save ', ' save ', $title);<br \/>\n$title = str_replace(' Since ', ' since ', $title);<\/p>\n<p>$title = str_replace(' Than ', ' than ', $title);<br \/>\n$title = str_replace(' The ', ' the ', $title);<br \/>\n$title = str_replace(' Through ', ' through ', $title);<br \/>\n$title = str_replace(' To ', ' to ', $title);<br \/>\n$title = str_replace(' Toward ', ' toward ', $title);<br \/>\n$title = str_replace(' Towards ', ' towards ', $title);<\/p>\n<p>$title = str_replace(' Under ', ' under ', $title);<br \/>\n$title = str_replace(' Underneath ', ' underneath ', $title);<br \/>\n$title = str_replace(' Unlike ', ' unlike ', $title);<br \/>\n$title = str_replace(' Until ', ' until ', $title);<br \/>\n$title = str_replace(' Up ', ' up ', $title);<br \/>\n$title = str_replace(' Upon ', ' upon ', $title);<\/p>\n<p>$title = str_replace(' Versus ', ' versus ', $title);<br \/>\n$title = str_replace(' Via ', ' via ', $title);<\/p>\n<p>$title = str_replace(' With ', ' with ', $title);<br \/>\n$title = str_replace(' Within ', ' within ', $title);<br \/>\n$title = str_replace(' Without ', ' without ', $title);<\/p>\n<p>$string = $title;<br \/>\n$my_array = explode(\" \", $string);<br \/>\n$my_count = count($my_array);<br \/>\n$my_last = $my_count -1;<br \/>\n$last_word = ucfirst($my_array[$my_last]);<br \/>\n$my_array[$my_last] = $last_word;<br \/>\n$i=0;<br \/>\n$new_string = \"\";<br \/>\nwhile($i &lt; $my_count){<br \/>\n$new_string = $new_string.\" \".$my_array[$i];<br \/>\n$i++;<br \/>\n}<br \/>\n$title = ucfirst($new_string);<br \/>\nreturn ($title);<br \/>\n}<br \/>\n\/\/provided by: www.codebangers.com<br \/>\n<\/code><\/p>\n<p>There it is, stick this in your functions.php file and give it the &#8220;title&#8221; text you want to appear correctly, rest easy knowing your titles are pro.<\/p>\n<p>For those of you who have never used PHP functions, you put the above code in a &#8220;functions.php&#8221; file and include it like this in your PHP:<\/p>\n<p><code><br \/>\ninclude_once('htdocs\/functions.php');<br \/>\n<\/code><\/p>\n<p>Next, for this particular function, you would call to it like so:<\/p>\n<p><code><br \/>\nucTitle($title);<br \/>\n<\/code><\/p>\n<p>With &#8220;$title&#8221; being what is getting corrected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes a developer has to get their titles from crazy sources that aren&#8217;t always\u00a0capitalized correctly. Nobody wants to change all these titles [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":300,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,4,31],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howto","category-php","category-programming"],"_links":{"self":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":19,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/posts\/299\/revisions\/318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/media\/300"}],"wp:attachment":[{"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codebangers.com\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}