0

I want to check the user entered characters are lowercase or uppercase. If it is lowercase i want to change into uppercase. If it is uppercase i want to change into lowercase.

<?php
$string=$_POST['string'];
$arr=str_split($string);
$arrlen=strlen($string);
$arrcaps=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"," ");
$arrsmall=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"," ");   
//print_r($arrsmall);

for($i=0;$i<$arrlen;$i++)
{
for($j=0;$j<27;$j++)
{
if($arr[$i]==$arrcaps[$j])
{
echo $arrsmall[$j];
}
}
for($k=0;$k<27;$k++)
{
if($arr[$i]==$arrsmall[$j])
{
echo $arrcaps[$j];
}
}
}
?>

I execute the above program. it change the uppercase characters to lowercase. But it not change the lowercase characters to uppercase.

Where i did mistake. solve this problem.

Thanks in advance...

CJ Ramki
  • 2,620
  • 3
  • 25
  • 47

6 Answers6

3
$switched = strtolower($string)^strtoupper($string)^$string;

This can be used as function:

echo changecase('a');//A
echo changecase('A');//a

function changecase($str){
  return strtolower($str)^strtoupper($str)^$str;
}
ironcito
  • 807
  • 6
  • 11
2

use

bool ctype_upper($string) — Check for uppercase character(s)

if ( ctype_upper($letter) )
{
   strtolower($letter);
}
else
{
   strtolower($letter);
}
gskema
  • 3,141
  • 2
  • 20
  • 39
2

If you can have only lowercased or only uppercased:

$result = (strcmp(strtoupper($string),$string))?strtoupper($string):strtolower($string);
Volvox
  • 1,639
  • 1
  • 12
  • 10
1

if($arr[$i]==$arrsmall[$j]) { echo $arrcaps[$j]; }

should be

if($arr[$i]==$arrsmall[$k]) { echo $arrcaps[$k]; }

case solved

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
1
<?php
$string=$_POST['string'];
print strtolower($str) ^ strtoupper($str) ^ $str;

I referred our stackoverflow website in another link...I tried this. It gives the answer... Thanks to all for the support...

CJ Ramki
  • 2,620
  • 3
  • 25
  • 47
1
$word = "AlPhAbEtIcIsE";
$word = strtr(
    $word,
    array_combine(
        array_merge(range('A','Z'),range('a','z')),
        array_merge(range('a','z'),range('A','Z'))
    )
);
var_dump($word);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385