Capitalizing the First Letter of Words
When it is necessary to capitalize the first letter of a word, this article contains examples to do that with PHP and with JavaScript.
When data is put into a database, it is nice to have it correctly capitalized. When an email sent as a response to an inquiry, it should be professionally presented. Similar with software-generated thank-you web pages.
Data obtained from web page forms may need capitalization correction. Data obtained as lists from third-party vendors or by compiling contact information of participants in a discussion may need capitalization attention.
Examples of specific data that may need correction are the name of a state or province. Also people names and the titles of books or articles.
What you'll find in this article is, first, how to capitalize a single word. Then, how to capitalize the first letter of individual words of strings such as names or titles.
The Functions
Below, you'll find two functions for each of the languages (PHP and JavaScript). Select the set of functions that belong to your preferred language.
The JavaScript Functions
<script type="text/javascript"> function FirstLetterCap(string) { return string.charAt(0).toUpperCase() + string.slice(1); } function AllLowerExceptFirstLetter(string) { string = string.toLowerCase(); return FirstLetterCap(string); } </script>
The PHP Functions
<?php function FirstLetterCap($string) { return ucfirst($string); } function AllLowerExceptFirstLetter($string) { $string = strtolower($string); return FirstLetterCap($string); } ?>
Those are copy-and-paste functions. Select the set you want to work with, JavaScript or PHP, and paste them into your test page.
For easier explanation, I made the function names the same for both languages.
The FirstLetterCap() function turns the first letter of a word into a capital letter — if the first letter was a lower-case letter. This function affects only the first character of the word. All other charcters remain as is. "capital" would be come "Capital". "camelCase" would become "CamelCase". (A number or punctuation as first character, of course, cannot be capitalized.)
The AllLowerExceptFirstLetter() function first turns all capital letters of the word into lower-case. Then, it capitalizes the first letter. "CAPITAL" would be come "Capital". "camelCase" would become "Camelcase".
Capitalizing a Single Word
For ease of copying and pasting, the example code will contain the above functions.
Below is the code to capitalize the first letter of the word "myWord". The functions presented above are regular code text. But the example code is bolded.
The JavaScript "myWord" Example Code
<script type="text/javascript">
alert( FirstLetterCap("myWord") );
function FirstLetterCap(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
function AllLowerExceptFirstLetter(string)
{
string = string.toLowerCase();
return FirstLetterCap(string);
}
</script>
The PHP "myWord" Example Code
<?php
echo( FirstLetterCap('myWord') );
function FirstLetterCap($string)
{
return ucfirst($string);
}
function AllLowerExceptFirstLetter($string)
{
$string = strtolower($string);
return FirstLetterCap($string);
}
?>
Both of those convert "myWord" to "MyWord". The JavaScript presents the result as a browser alert. The PHP publishes the result on the web page.
To convert "myWord" to "Myword", you would specify function AllLowerExceptFirstLetter() instead of the FirstLetterCap() function used in the above examples.
Capitalizing Individual Words of Strings
Now, let's use a multi-word example, "I am free", and convert each word. Below is the code for each programming language example.
The JavaScript "I am free" Example Code
<script type="text/javascript">
var title = "I am free";
var words = title.split(" ");
for( var i=0; i<words.length; i++ ) { words[i] = FirstLetterCap(words[i]); }
title = words.join(" ");
alert( title );
function FirstLetterCap(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
function AllLowerExceptFirstLetter(string)
{
string = string.toLowerCase();
return FirstLetterCap(string);
}
</script>
The PHP "I am free" Example Code
<?php
$title = 'I am free';
$words = explode(' ',$title);
for( $i=0; $i<count($words); $i++ ) { $words[$i] = FirstLetterCap($words[$i]); }
$title = implode(' ',$words);
echo( $title );
function FirstLetterCap($string)
{
return ucfirst($string);
}
function AllLowerExceptFirstLetter($string)
{
$string = strtolower($string);
return FirstLetterCap($string);
}
?>
Both of those convert "I am free" to "I Am Free". As in the previous examples, the JavaScript presents the result as a browser alert and the PHP publishes the result on the web page.
Also as in the previous examples, to convert all letters to lower-case and then convert the first letter to upper-case, specify function AllLowerExceptFirstLetter() instead of the FirstLetterCap() function.
With those two functions, whether you prefer to program with PHP or with JavaScript, you have the means to programatically capitalize words and strings of words.
(This content first appeared in Possibilities newsletter.)
Will Bontrager