How to change string’s case with PHP functions and with CSS


For a PHP developer, working with databases, arrays, doing string manipulations, etc are common daily tasks. While working with strings, you may sometime require changing case of the sting from lower to upper and vice versa or even you may need to change each word’s case in the given string. You can achieve all these by using following 5 PHP built in functions or you can use CSS property text-transform. Lets start with PHP functions.

1. strtolower() – convert a complete string into lowercase.

Example:


2. strtoupper() – convert a complete string into uppercase. Just opposite of strtolower()

Example:


3. lcfirst() – Make a string’s first character lowercase.

Example:


4. ucfirst() – Make a string’s first character uppercase. Just opposite of lcfirst().

Example:


5. ucwords() – Uppercase the first character of each word in a string.

Example:


Source: php.net

Alternately, You can use CSS to change the text case. Use text-transform property to change the case in three ways:

Lower case: Makes all text in lower case.

.lowerText{
  text-transform: lowercase;
}

Upper case: Makes all text in upper case.

.upperText{
  text-transform: uppercase;
}

Capitalize: Makes uppercase the first character of each word in a string. It works exactly the same way the ucwords() php’s function does.

.capitalizeText{
  text-transform: capitalize;
}

Related posts:

About Sach

Sachin is a web application developer, technology blogger and web addict! He has over 6 years of web development experience and he writes tutorials primarily focused on LAMP, Ajax, Api's, jQuery etc. He is usability expert and he always likes to share his knowledge with people.

, , ,