0% found this document useful (0 votes)
73 views1 page

Capitalizing The First Letter - Ucwords: PHP Code

The document discusses the PHP ucwords function, which capitalizes the first letter of each word in a string. It demonstrates using ucwords alone, which capitalizes all letters not just the first, and using strtolower first followed by ucwords to ensure only the first letter of each word is capitalized.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views1 page

Capitalizing The First Letter - Ucwords: PHP Code

The document discusses the PHP ucwords function, which capitalizes the first letter of each word in a string. It demonstrates using ucwords alone, which capitalizes all letters not just the first, and using strtolower first followed by ucwords to ensure only the first letter of each word is capitalized.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Capitalizing the First Letter - ucwords

Titles of various media types often capitalize the first letter of each word and PHP has a time-saving
function that will do just this.

PHP Code:
$titleString = "a title that could use some hELP";

$ucTitleString = ucwords($titleString);
echo "Old title - $titleString <br />";
echo "New title - $ucTitleString";

Display:
Old title - a title that could use some hELP
New title - A Title That Could Use Some HELP

Notice that the last word "hELP" did not have the capitalization changed on the letters that weren't first,
they remained capitalized. If you want to ensure that only the first letter is capitalized in each word of your title,
first use the strtolower function and then the ucwords function.

PHP Code:
$titleString = "a title that could use some hELP";

$lowercaseTitle = strtolower($titleString);
$ucTitleString = ucwords($lowercaseTitle);
echo "Old title - $titleString <br />";
echo "New title - $ucTitleString";

Display:
Old title - a title that could use some hELP
New title - A Title That Could Use Some Help

You might also like