Skip to main content

If you have a website 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:

  1. User enters their name as “paul jones”.
  2. 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.
  3. 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:

function surname($surname)
{
   // correct capitalisation on all surnames
   $wspaces = array(” – “, ” ‘ “, ” Mc “, ” Mac “);
   $wospaces = array(“-“, “‘”, “Mc”, “Mac”);
   $surname = str_replace($wspaces, $wospaces, ucwords(strtolower(str_ireplace($wospaces, $wspaces, $surname))));
   return $surname;
}

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

Paul (@paulfp) is the main presenter of the award-winning Switched On Network YouTube Channel, which covers a variety of interesting topics usually relating to his love of technology and all things geeky. He also founded and runs Innobella Media, where he leads in all aspects of video production, video editing, sound & lighting. A father of 3 children including twins, his hobbies used to include photography, playing the drums and cycling. With a degree in Modern European Languages, Paul speaks French, Spanish and a little bit of Italian, and holds dual British & Irish citizenship.

3 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu
Freelance Camera Operator, Videographer, Vision Mixer, Sound Engineer, Video/Audio Editor & Photographer