PHP – String Case – Capitalization

by Hiroshi on June 23, 2008

in Capitalization, Scripts, String Case, Tricks

String to Upper Case – strtoupper

The strtoupper function takes one argument, the string you want converted to upper case and returns the converted string. Only letters of the alphabet are changed, numbers will remain the same.

<?php
$originalString = "String Capitalization 5654";
$upperCase = strtoupper($originalString);
echo "Old string - $originalString <br />";
echo "New String - $upperCase";
 
// Output:
// New String - STRING CAPITALIZATION 5654
?>

String to lower Case – strtolower

The strtolower function also has one argument: the string that will be converted to lower case.

<?php
$originalString = "String Capitalization 1234";
$lowerCase = strtolower($originalString);
echo "Old string - $originalString <br />";
echo "New String - $lowerCase";
 
// Output:
// New String - string capitalization 1234
?>

Capitalizing the First Letter – ucwords

<?php
$titleString = "a title that could use some hELP";
$ucTitleString = ucwords($titleString);
echo "Old title - $titleString <br />";
echo "New title - $ucTitleString";
 
// Output:
// New Title - New title - A Title That Could Use Some HELP
?>

Related Posts

Previous post:

Next post: