Forum Moderators: coopster & phranque

Message Too Old, No Replies

Capitalize first letter of each word in a string

         

MatthewHSE

3:08 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know diddly about Perl, but I need to make a slight modification to a Perl script on my site. It handles strings of text, and the following line is used to capitalize the first letter in the string:

$KeyWord = ucfirst($KeyWord);

Is there any easy way to modify that so that it will capitalize the first letter of each word in the string, like ucwords() in PHP? (I did a search for 'perl ucwords' and got some results, but when I tried it, it caused a software error in the script.)

Thanks,

Matthew

coopster

3:19 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I believe ucwords is part of a CPAN module, Interpolation. You could either use the module or roll your own 'ucwords' function.
my $string = lc shift; 
$string =~ s/\b(\w)/\u$1/g;

perl_diver

8:13 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



I don't think coopsters code will work as posted, but should with a little modification, this will work:

my $string = 'this is a string of text';
$string =~ s/\b(\w+)\b/ucfirst($1)/ge;
print "$string\n";

but without seeing the actual data it's hard to say how effective it will be.

coopster

9:20 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, perl_diver.

You are correct, that code is not a working snippet, merely a snapshot of the function that I located in the Interpolation module. Thanks for the help there ;-)

perl_diver

7:06 am on Jan 6, 2006 (gmt 0)

10+ Year Member



Thanks for the welcome. Sorry for the confusion, I see now that you were informing the user that they could use the Interpolation module, in which case your code might have worked as-is, I'm not sure, I never used that module, but it does look interesting.

My only other concern is that it is not a core module and has a dependency: HTML::Entities , both would have to be installed by the user, which seems like more effort than necessary since a simple regexp will suffice for this situation.

coopster

3:31 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I agree wholeheartedly. I would grab that simple regex, roll my own function, and go from there ;-)

pageoneresults

3:48 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Off topic but, you can do this with CSS too...

.ttc{text-transform:capitalize;}

coopster

4:37 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hey! Not a day goes by!
as he logs CSS tip in memory bank ...

pageoneresults

4:40 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



as he logs CSS tip in memory bank ...

lol! Was that you I just saw in my logs not long ago? Actually it wasn't, but it was someone reading this topic. ;)

css+upper+case

MatthewHSE

9:27 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for all the ideas guys. Overall, the CSS tip seems the simplest. I'll try them all and see what seems to work best.

Thanks again!

mvander

5:22 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



Thanks, that css trick worked perfect for me!