Sunday 4 April 2010

Making a String Uppercase, Lowercase, or Capitalized!!

Ok in this post I am going to show you how to Uppercase, Lowercase, or Capitalize a string or a word, I mean, making a work UPPERCASE or lowercase or Capital Word. Hmm now I guess I got things much clearer for you.

Now we'll first start with uppercase, the way we do that is:

$string = "heY hOw arE yoU doinG?";
echo strtoupper($string);
// Displays "HEY HOW ARE YOU DOING?"

What we did here is we used a PHP function called string to upper or strtoupper()

Likewise, we have the string to lower i.e strtolower()
echo strtolower($string);
// Displays "hey how are you doing?"

And yes, of course, we have the last one... called Uppercase. What this will do is uppercase everything:

$string="hoW ARE yOU doInG?";
echo ucwords(strtolower($string));
// Displays "Hey How Are You Doing?"

Important: Here, we have used a function ucwords() but before we did that we used the strtolower() function to lower case all the characters in the string... then.... we Capitalized the words in the string. Make sense?

I hope it did.

Enjoy and leave comments below!

No comments :