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