Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting a string to capitalize first letter of each word 2

Status
Not open for further replies.

kelly5518

Technical User
Aug 1, 2005
72
US
Hi,

I tried doing this through CSS, but because of an apparent bug in CSS, it doesn't convert the string when it originates as all caps. Strange, I know, but if the text entered is all lowercase or mixed case it works great, but when it's all uppercase, the text-transform: capitalize feature doesn't work.

Anyway, I'd like to try to get it done server-side, but I'm not a programmer and I don't want to mess something up. I was wondering if someone would be kind enough to look at the following code and let me know if there's a simple way to accomplish what I want.

Code:
sub Update {
    my $default_permit_html = 0;
    if($FORM{action} eq 'process_update') {
	delete $FORM{action};
	if($FORM{update_text} =~ /[a-zA-Z1-9]/) {
	    my $file = new MongerFile("$Data/defaultdata.ftf");
	    $file->unlink;
	    $file->write($FORM{update_text});
	    $WARN = "Update Complete. The new data will now be displayed on the live page.<br>You can view the updated page <a href=\"/home.shtml\" target=\"_blank\">here</a>.";
	} else {
	    unlink("$Data/defaultdata.ftf");
	    $WARN = "Update Complete. Since your submission was blank,<BR>there is no data to display on the page, but you can view the page <a href=\"/home.shtml\" target=\"_blank\">here</a>.";
	}
	&Update;
    } else {
	my $file = new MongerFile("$Data/defaultdata.ftf");
	&PrintAdminHead;
	print "<CENTER><B>\n";
	if($file->{exists}) {
	    print "This information was last updated:<BR>", $file->modified, ".</B><BR>\n";
	}

Thanks for any suggestions!

 
Sorry, missed the /e

Code:
sub Update {
    my $default_permit_html = 0;
    if($FORM{action} eq 'process_update') {
    delete $FORM{action};
    if($FORM{update_text} =~ /[a-zA-Z1-9]/) {
        my $file = new MongerFile("$Data/defaultdata.ftf");
        my $new_update_text = $FORM{update_text};
        $new_update_text =~ s/(\w+)/ucfirst(lc($1))/g[red]e[/red];
        $file->unlink;
        $file->write($new_update_text);
        $WARN = "Update Complete. The new data will now be displayed on the live page.<br>You can view the updated page <a href=\"/home.shtml\" target=\"_blank\">here</a>.";
    } else {
        unlink("$Data/defaultdata.ftf");
        $WARN = "Update Complete. Since your submission was blank,<BR>there is no data to display on the page, but you can view the page <a href=\"/home.shtml\" target=\"_blank\">here</a>.";
    }
    &Update;
    } else {
    my $file = new MongerFile("$Data/defaultdata.ftf");
    &PrintAdminHead;
    print "<CENTER><B>\n";
    if($file->{exists}) {
        print "This information was last updated:<BR>", $file->modified, ".</B><BR>\n";
    }

Trojan.
 
broken down:-

Code:
s - substitute
/ - delimiter start of SEARCH
( - capture to $1 BEGIN
\w+ - [i]word[/i] characters - NOT spaces
) - capture to $1 END
/ - delimiter end SEARCH & start of REPLACE
g - global ... do as many times as possible
e - evaluate the expression - required due to the lc($1) which then applies the lowercase


Kind Regards
Duncan
 
Ok, I just want to make sure I'm putting it in the right place, so referring to the relevant portion of the subroutine above, it would look like this? (bold text is where I added it)

Code:
sub Update {
    my $default_permit_html = 0;
    if($FORM{action} eq 'process_update') {
    delete $FORM{action};
    if($FORM{update_text} =~ /[a-zA-Z1-9]/) {
    	[b]$FORM{update_text} =~ s/(\w+)/ucfirst(lc($1))/ge;[/b]
	my $file = new MongerFile("$Data/defaultdata.ftf");
	$file->unlink;
	$file->write($FORM{update_text});

Thanks...
 
Sorry, I guess I was writing while you guys were posting and didn't see your posts in time. I'll give it a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top