Capitalizing the first letter of each word – a common need for sure. There is a function for this on cflib but it didn’t work for me. No errors but it wasn’t returning what I expected. I came across this function that works, and does so with much less code:
<cffunction name="CapFirst" access="public" output="false" returntype="String">
<cfargument name="inputString" required="false" type="String" default="" />
<cfreturn rereplace(lcase(arguments.inputString), "(\b\w)", "\u\1", "all") />
</cffunction>
I found this snippet on the WeCodeThings blog. Good stuff.
Jake says:
You can do this with CSS too: element { text-transform:capitalize; }
17 March 2010, 9:32 pmRyan says:
Thats a great idea, that will work in many cases but it doesn't seem to work where I needed it yesterday – inside a select option.
18 March 2010, 7:24 amziggy says:
That ruins acronyms and other all-cap words.
20 March 2010, 11:21 pmRyan says:
True, perhaps it could be improved upon. In my case the text I was working on was all caps already, so adding a check to leave all cap words would not have worked for me.
21 March 2010, 8:44 amShane says:
@Jake:
29 March 2011, 5:20 amThanks for the CSS version of this! Exactly what I was looking for. 🙂
Matt Wadsworth says:
Hi Ryan,
Thanks a lot for pointing me in the direction of the We Code Things blog.
That is going to come in very handy indeed.
Best
-Matt
29 March 2011, 10:03 amBrandon says:
I just started learning CF and this is exactly what I was looking for. Thanks to Jake for his suggestion too.
If anyone is looking to do this in php it's simply ucwords($variable or text);
Thanks,
29 March 2011, 12:18 pmBrandon
Webmaster, http://www.toprankconsultants.com
Bobby Tan says:
@Jake: That's the first thing I thought of too. Great minds and all that. 🙂
@Brandon: Thanks for the PHP version.
The WeCodeThings blog is an excellent resource. Thanks!
30 March 2011, 6:51 pm