How to Automatically Capitalize a String in PHP

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.

UCWords in particular is an easy function to learn since it only accepts one argument: the string that you wish to manipulate. If done correctly, the function will then be able to capitalize the first letter of every word in a given string. Similar functions such as StrToUpper will actual capitalize each letter in the string, so be sure to make the distinction between the bunch.

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.

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.

Closing Comments

In the end, UCWords is quite a benefit to web developers. It’s quite a help for everyone from editors to the general webmaster, simply because attention doesn’t have to be paid to titles or database input. For more information on this function, you should consult the PHP manual or search for further examples of usage in trusted PHP resources around the Internet.



Source link