Play with string: String conversion in PHP

Let’s two string

$s=”convert upper case to lower case, lower case to upper case and more!”;

$p=”Convert Case – Convert upper case to lower case, lower case to upper case and more! AccIdentALly left the caPs Lock on and typed something, but can’t be bothered to start again and retype it all?
Simply follow these following easy steps to COnvert your text to exactly how you want it. So, lEt’s try!!”;

And then,

echo strtolower($p);
Output:-
convert case – convert upper case to lower case, lower case to upper case and more! accidentally left the caps lock on and typed something, but can’t be bothered to start again and retype it all? simply follow these following easy steps to convert your text to exactly how you want it. so, let’s try!!

——————————————————-

echo strtoupper($p);
Output:-

CONVERT CASE – CONVERT UPPER CASE TO LOWER CASE, LOWER CASE TO UPPER CASE AND MORE! ACCIDENTALLY LEFT THE CAPS LOCK ON AND TYPED SOMETHING, BUT CAN’T BE BOTHERED TO START AGAIN AND RETYPE IT ALL? SIMPLY FOLLOW THESE FOLLOWING EASY STEPS TO CONVERT YOUR TEXT TO EXACTLY HOW YOU WANT IT. SO, LET’S TRY!!

——————————————————————-
echo ucfirst($s);
Output:-

Convert upper case to lower case, lower case to upper case and more!

————————————————————————–
echo ucwords($s);

Output:-

Convert Upper Case To Lower Case, Lower Case To Upper Case And More!

————————————————————————–
echo strtosentencecase($tp);

Output:-

Convert case – convert upper case to lower case, lower case to upper case and more! Accidentally left the caps lock on and typed something, but can’t be bothered to start again and retype it all? Simply follow these following easy steps to convert your text to exactly how you want it. So, let’s try!!

Function:-

function strtosentencecase($s) {
$str = strtolower($s);
$cap = true;
for($x = 0; $x < strlen($str); $x++){
$letter = substr($str, $x, 1);
if($letter == “.” || $letter == “!” || $letter == “?”){
$cap = true;
}elseif(trim($letter) != “” && $cap == true){
$letter = strtoupper($letter);
$cap = false;
}
$ret .= $letter;
}
return $ret;
}

——————————————————————————

Advertisements

One thought on “Play with string: String conversion in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s