Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryGenerators and Converters

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC