1. Computing

Discuss in my forum

Changing the capitalization of stings

All capitals, all lower case, capital words or capital stings

By , About.com Guide

Using simple PHP code we can manipulate the capitalization of strings. We may want to do this so that data is stored in a uniform format (for example all names are stored in all capital letters.) Or we might want to use this function to convert text being used for searches so that searches are not case sensitive. Regardless of your reason, there are several ways to manipulate the string.

Making the string all capital letters: You can simulate the CAPSLOCK key using the strtoupper PHP function. This will make every letter in the string a capital letter.

Example: Print(strtoupper("I am using strtoupper"));>

Result: I AM USING STRTOUPPER

Making the string all lower case letters: You can also force all letters in a string to be lower case. This is done with the strtolower PHP function.

Example: Print(strtolower("I AM USING STRTOLOWER"));>

Result: i am using strtolower

Capitalizing each word: You can choose to only capitalize the first letter of every word. This works great when working with a list of names or cities. This is done with the ucwords PHP function.

Example: Print(ucwords("angela, bradley, alexa, jason"));>

Result: Angela, Bradley, Alexa, Jason

Capitalizing each string: You can also choose to only capitalize the first letter of each string. This could be used to make sure sentences (as separate strings) are capitalized. This is done using the ucfirst PHP function.

Example: Print(ucfirst("this is my sentence."));>

Result: This is my sentence.

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. Manipulating Capitalization in Strings

©2013 About.com. All rights reserved.