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...

6
  • you want all charters to change to be uppercase/lowercase ? give ab example of what you want Commented Aug 6, 2013 at 9:02
  • 1
    code indentation would be nice.
    – ciruvan
    Commented Aug 6, 2013 at 9:03
  • 1
    you can use php function strtolower and strtoupper. Commented Aug 6, 2013 at 9:04
  • but in this program i want to check each character. if i use strtolower or strtoupper functions, how can i check the each character lower or upper case?
    – CJ Ramki
    Commented Aug 6, 2013 at 9:10
  • Perhaps strtr() could be used rather than this collection of loops
    – Mark Baker
    Commented Aug 6, 2013 at 9:15

6 Answers 6

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;
}
0
2

If you can have only lowercased or only uppercased:

$result = (strcmp(strtoupper($string),$string))?strtoupper($string):strtolower($string);
1
  • This is also the cool idea...
    – CJ Ramki
    Commented Aug 6, 2013 at 9:18
2

use

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

if ( ctype_upper($letter) )
{
   strtolower($letter);
}
else
{
   strtolower($letter);
}
1

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

should be

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

case solved

0
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);
0
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...

2
  • duplicate of @ironcito's answer
    – user1646111
    Commented Aug 6, 2013 at 9:21
  • I think the same this is the best way for my problem...
    – CJ Ramki
    Commented Aug 6, 2013 at 9:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.