When users fill out my registration form I get all kinds of formats:
firstname lastname
FIRSTNAME LASTNAME
Sometimes they add an extra space after their name too. Is there a good php function that will strip out extra spaces as well as use good capitalization? It needs to take into consideration names like McDowell that properly use two capital letters.
Is it possible for me to write some code that would check to see if the string is in uppercase, and if so, convert it? That way if the string is something like McDonell, I won’t convert it.
Which and how many characters do you want to check if they are already in uppercase? Or you want to check all the characters in a word or whole phrase?
There are several PHP functions to check for upper case characters, there is even a function {http://php.net/manual/en/function.ucfirst.php} to make the first character upper case.
If you want to allow McDonnell type names, a regexp might be more suited.
Can you help me understand why the output of this script is only, “testing…123” ? Here is my code:
Thanks!
<?php
$firstname = "BILLY JONES";
if (ctype_upper($firstname)) {
// Fix the name because it is all upper case
//$firstname = ucwords(strtolower($firstname));
print ("UPPERcase");
}
if (ctype_lower($firstname)) {
// Fix the name because it is all lower case
//$firstname = ucwords($firstname);
print ("lowercase");
}
print ("testing.....123");
?>