Friday, October 24, 2008

An Easy Way To Force A PHP String Into Uppercase

By Chris Channing

Developing a web application in PHP takes a long time, depending on the complexity of the project. Thus, programmers should make use of time-saving functions that allows them to improve efficiency both for them and others who work with what they have created. One such function to delve into is UCWords- a particularly nice time-saver.

The UCWords function only accepts one argument, so it's about as basic as you can get in terms if simplicity. The function will capitalize the first letter of every word in a string supplied to it. There are very similar functions that may convert every letter to uppercase or even lowercase, so don't get confused between the wide variety of functions at your disposal.

One of the most popular applications of the UCWords function is to ensure that user input from a form is uniform with other data received. This is quite easy to see in social networking websites, which typically automatically capitalize the name of their users on their profiles. When users receive an email, they'll also notice their name is capitalized regardless if they capitalized it upon registration.

Webmasters are quite familiar with the Meta tags used to tell search engines what their website is about, and how to display the listing in the search results. UCWords is used in this case to ensure the "Title" attribute of the website is always capitalized. This allows for better efficiency by allowing the designer to disregard capitalizing words, but also ensures there are no errors in letter casing.

By applying the UCWords function to a custom-made function, we can also alter strings to keep certain words lowercased. This is especially important for proper news resources, who know that proper title case is going to mean that some prepositions or words need to remain in lower case. By initializing an array of prepositions and selectively making them lowercased, web developers can ensure that there are no mistakes in how a title should appear.

It's important to note that the UCWords function isn't always going to render foreign characters correctly. While this should be fixed in the next version of PHP version thanks to the fact that multi-language support is being added, for now users of certain text encoding structures will have to create custom functions to get this to work properly. This is obviously a setback, but this will not affect the majority of web developers around the world.

Final Thoughts

UCWords has much functionality to take advantage of. If you think you would like to give it a try, consider looking around for a specific tutorial with examples on coding practice. - 15634

About the Author: