Title case function in PHP based on grammatical rules for capitalizing the words in a title

Creating a Title Case Function with PHP Based on Grammatical Rules.

The common way that programmers implement title case is the most simple way, they capitalize the first letter of every word. But what about titles like the title above where the "on" and "with" are not capitalized? This is called "Headline Case." There are many different articles on the web about using PHP to title case a string. Most don't try to encompass the more complicated grammatical rules that govern title case. I have written a function that follows most of the grammatical rules of "Headline Case" If you have titles where you would have exceptions to these rules, I have provided the tags < no_parse > as a feature of the function below. In the case were you want to take over formatting and override the parser you can uses these tags to exempt it from the title casing parser. This way 90% of all situations are covered after which you only have to manually tweak a few entries. This takes so much of the grunt work out of data entry. In any case the tags are provided for your convenience. You may notice that the New York Times is inconstant on which style rules they use for title case (at least it was at the time of this writing). If they used an algorithm like mine there would be automatic standardization. I suggest you use the following function to make sure your content titles are standardized and let your authors who are the experts in writing opt out of the title case rules with the < no_parse > tags at their discretion. I may have missed special cases. I encourage you to use your Github account to fork the code below to improve it. That is, should you find your self more grammatically knowledgeable than I.




comments powered by Disqus