Converting the character case in PHP



Sometime we need to convert case of the word o variable we got in PHP. PHP have a built in function for converting the case of the word, Which can be use as below code.

Some example are, we save all user name in database and after calling from database, we don't know what will be the output of name in the sense of character case, as user can enter his name all in capital, all in small or can insert into first character capitcal.

So we will manipulate these as follow:

  • First we convert in to lower case, suppose we are getting  variable i.e.  $username, so we do $username = strtolower($username);
  •  Then we convert it to according to our need as follow:
  • If want only first character capital:       $username = ucfirst($username);
  • If want only first character capital:       $username = strtoupper($username);
  • We can also do it by one line as :
  • $username = ucfirst(strtolower($username));
  • $username = strtoupper(strtolower($username));
############  Code ############ 
<?php

$word = "word";

echo "The Word is : ".$word;

echo "<br/><br/><b>Word convert all character to capital.</b><br/>";

echo strtoupper($word);

echo "<br/><br/><b>Word convert all character to small.</b><b/>";

echo strtolower($word);

echo "<br/><br/><b>Word convert first character to capital.</b><br/>";

echo ucfirst($word);

echo "<br/><br/><b>Word convert first character to small.</b><br/>";

echo lcfirst($word);


echo "<hr/>";

$string = "this is a sentence, all in lower case.";

echo "The Sentence is : ".$string;

echo "<br/><br/><b>Sentence convert all word 1st character to capital.</b><br/>";

echo ucwords($string);

?>

############  Code ############ 
############  O/P ############ 

The Word is : word

Word convert all character to capital.
WORD

Word convert all character to small.
word

Word convert first character to capital.
Word

Word convert first character to small.
word

The Sentence is : this is a sentence, all in lower case.

Sentence convert all word 1st character to capital.
This Is A Sentence, All In Lower Case.



############  O/P ############ 

Comments

  1. MMCMC Casino & Resort - DrmCD
    Dr. MMC 수원 출장샵 Casino & Resort (formerly the MMC Casino and Resort in Tunica) 군포 출장샵 is an Indian 영주 출장마사지 casino and hotel 동해 출장샵 located in Tunica, 천안 출장샵 Mississippi,

    ReplyDelete

Post a Comment

Popular posts from this blog

Page Unload Show Notification to user

Function Oriented File Upload PHP

Upload File Using PHP - Example For Image