Skip to content

Instantly share code, notes, and snippets.

@oceanapplications
Created June 29, 2015 04:02
Show Gist options
  • Save oceanapplications/44d69d83971d3248b60a to your computer and use it in GitHub Desktop.
Save oceanapplications/44d69d83971d3248b60a to your computer and use it in GitHub Desktop.
String case inversion script
//Version 1, simple and performs about 40 times better than version 2
$input = "i FORGOT CAPLOCK";
$output = "";
foreach(str_split($input) as $letter)
{
if(ctype_upper($letter))
{ $output .= strtolower($letter);}
else { $output .= strtoupper($letter); }
}
echo $output;
//Version 2, not really a lot of point to it, it's slow and more complicated
$text = str_split("i FORGOT CAPLOCK");
foreach($text as $position => $input)
{
if(ctype_upper($input))
{ $text[$position] = strtolower($input);}
else { $text[$position] = strtoupper($input); }
}
echo implode($text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment