Source Code
Below is the PHP Source Code for the rot_n() function used on this site.
<?php
function rot_n($string, $n, $n_numeric=FALSE)
{
$result = “”;
$length = strlen($string);
for($i = 0; $i<$length; $i++)
{
$ascii = ord($string{$i});
$rotated = $ascii;
# Capital letters are 65 to 90
if ($ascii>64 && $ascii<91)
{
$rotated = $rotated + $n;
$rotated > 90 && $rotated += -90 + 64;
$rotated < 65 && $rotated += -64 + 90;
}
# Lowercase letters are 97 to 122
elseif ($ascii>96 && $ascii<123)
{
$rotated = $rotated + $n;
$rotated > 122 && $rotated += -122 + 96;
$rotated < 97 && $rotated += -96 + 122;
}
# Numeric digits are 48 to 57
if($n_numeric AND $ascii>47 AND $ascii<58)
{
$rotated = $rotated + $n_numeric;
$rotated > 47 && $rotated += -57 + 47;
$rotated < 58 && $rotated += -47 + 57;
}
$result .= chr($rotated);
}
return $result;
}
?>