How to Automatically Correct Capitalisation of Users' Surnames in your Web Site's Database
If you have a web site where users are invited to sign up and create an account, or leave a comment at the end of an article or blog post, it's a good idea to do a little bit of processing on the text they enter in for their name, to ensure the capitalisation looks right. This keeps a consistent theme throughout your web site and keeps the whole thing looking much more professional.
Using PHP, it's easy to do this most of the time. All you need to do is firstly convert the whole string to lowercase (so you start with a "clean canvas") using the strtolower() function and then use the ucwords() function to capitalise the first letter of each word in the string.
A typical scenario would go something like this:
- User enters their name as "paul jones".
- strtolower()makes sure all characters in the string are lowercase, just to be sure. If I'd entered my name as "pAul" this would get corrected.
- ucwords()then turns this into "Paul Jones" - much nicer.
This approach works fine most of the time, but if you're an awkward so-and-so (like me) and have a double-barrelled surname then this causes problems. The same can be said for surnames which have Mc or Mac in, or surnames with apostrophes. The above would turn my surname into "Freeman-powell" instead of "Freeman-Powell", regardless of whether I entered it in correctly anyway! Clearly, that would not be a great experience for your visitor.
The following code snippet is a simple way I came up with to get around this problem and ensure that surnames displayed on your web site look neaty and tidy:
Basically, this function first adds spaces in around the parts which cause confusion (hypens, apostrophes, and Mc or Mac). It then capitalises the first letters in the same way, and then takes the spaces out again. Notice the use of the str_ireplace() function - the extra "i" in the function name makes it case-sensitive so that it doesn't undo all our work again.
Any surnames I've missed out? Let me know and I'll add them above.
Feel free to use this on your blog or web site, and please leave a comment below with a link to where you've used it!
Paul Freeman-Powell is a technology, software and hardware enthusiast. He founded and owns caeus.com Ltd. and works as a web developer, IT consultant and computer repair technician. In his spare time, he speaks French and Spanish fluently and is also a keen drummer and photographer (but not at the same time). His personal web site can be found at www.paulfp.net.
Your Comments
Copyright © caeus.com Ltd. 2011. This article and all its contents are the property of caeus.com Limited and are protected by copyright. You may not distribute, modify, transmit, reuse, repost, or use the content of this article for public or commercial purposes (including text and images) without written permission from caeus.com Limited. The views expressed in this article and sites linked to herein are solely those of the author or individual(s) providing them and do not reflect the opinions of caeus.com Ltd., its parents, affiliates or subsidies. Any trademarks and brands mentioned in this article are the property of their respective owners and their use does not imply any affiliation with, or endorsement by, caeus.com Limited. The content of this article is provided in good faith and for information purposes only; you follow any advice given entirely at your own risk and no responsibility can be taken for any consequences which may arise as a result of following advice given in this article.


1. At 08:38 on 23 Sep 2011 Trinity wrote:
I don't know who you wrote this for but you helped a brother out.