Click to See Complete Forum and Search --> : Capitalise string


TheSkyOrBust
07-17-2008, 01:23 PM
Being a bit of a Grammar Bully at heart, it annoys me when people don't capitalise their names when submitting a form, so I've come up with a PHP function to solve this problem.

<?php
function correct($i){

// Get first character of string
$ii = $i[0];

// Determine and replace first character
if($ii == 'a'){ $ii=A; }else
if($ii == 'b'){ $ii=B; }else
if($ii == 'c'){ $ii=C; }else
if($ii == 'd'){ $ii=D; }else
if($ii == 'e'){ $ii=E; }else
if($ii == 'f'){ $ii=F; }else
if($ii == 'g'){ $ii=G; }else
if($ii == 'h'){ $ii=H; }else
if($ii == 'i'){ $ii=I; }else
if($ii == 'j'){ $ii=J; }else
if($ii == 'k'){ $ii=K; }else
if($ii == 'l'){ $ii=L; }else
if($ii == 'm'){ $ii=M; }else
if($ii == 'n'){ $ii=N; }else
if($ii == 'o'){ $ii=O; }else
if($ii == 'p'){ $ii=P; }else
if($ii == 'q'){ $ii=Q; }else
if($ii == 'r'){ $ii=R; }else
if($ii == 's'){ $ii=S; }else
if($ii == 't'){ $ii=T; }else
if($ii == 'u'){ $ii=U; }else
if($ii == 'v'){ $ii=V; }else
if($ii == 'w'){ $ii=W; }else
if($ii == 'x'){ $ii=X; }else
if($ii == 'y'){ $ii=Y; }else
if($ii == 'z'){ $ii=Z; }else
{$ii=$ii; } // If not letter, leave as is

// Replace first character of string with new
$i[0] = $ii;

return $i;
}
?>

It works and I'm pleased but, knowing my luck, there is probably an existing function to do this. But I'd like to know what you all think. Criticisms and suggestions will be taken gratefully :cool: .

sstalder
07-17-2008, 02:24 PM
Yes in fact PHP has a function build in:

ucfirst($string);

TheSkyOrBust
07-17-2008, 02:44 PM
Well, that's typical... Nevermind, I've learnt because of this experience >_>

sstalder
07-17-2008, 02:48 PM
Haha, thats too funny. I can't tell you how many times I have been there man. Always try searching the PHP site before trying to get too crafty :)

Znupi
07-17-2008, 04:33 PM
Um, if they submit a full name (first & last), you might want to use ucwords() (http://php.net/ucwords) :)
Also, your function is pretty ugly. Here's a nicer one, using some ASCII :)
function correct($i) {
if ($i[0] >= 'a' && $i[0] <= 'z') $i[0] = chr(ord($i[0])-32);
return $i;
}

RPalmer303
07-18-2008, 05:16 PM
But then what if Wernher von Braun wants to sign up? I believe the lowercase "v" is valid in that case. And if I were him, I'd be annoyed when I got an email addressed to Mr. Von Braun.

International website users (not sure if you have any; this may be an unnecessary tangent) throw a wrench into American first/middle initial/last naming convention logic.

sstalder
07-18-2008, 11:14 PM
Then in that case you just need to apply some logic to determine what words can be capitalized.

felgall
07-19-2008, 06:48 PM
There are only really two ways to be certain you have it right.

1. Accept their name as they typed it. If they spell it with a lowercase letter it is because they want it that way.

2. Convert their entire name to uppercase. That way you don't have an issue with which letters should be in what case.