An Easy Way To Force A PHP String Into Uppercase

by Chris Channing

PHP, one of the Internet’s most popular development languages, has come a long way since its inception. We can now make use of nifty functions built into the language such as UCWords. This function in particular can save webmasters quite a bit of time in editing titles, descriptions, and editing user input.

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.

UCWords is used quite a bit in sanitizing database input for first and last names. Since not every registrant on a website is going to take the time to properly capitalize their name, we use the UCWords function to ensure a sense of conformity. This is especially useful in email marketing, where addressing a registrant by their lowercased name is going to come off as unprofessional.

UCWords is also used widely where no one really notices it. Search engines will commonly look at Meta tags to determine the “worth” of a website. Because webmasters like to maintain a professional look for their website, using UCWords on the backend allows the title of the website be properly displayed in search results no matter how it was entered into the Meta tags of the website.

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.

In Conclusion

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.

About the Author:

Leave a Reply