A nice, simple way of using rot-n. Allows for a lot of easy customisation, and works quickly. This function uses ASCII characters and values.
<?php
function rotn($val, $n) {
$min = 0;
$max = 127;
$final = '';
$new_string = str_split($val);
foreach ($new_string as $letter) {
$current = ord($letter);
$val = $current+$n;
if ($val >= $max) {
$val = $val - $max;
}
if ($val <= $min) {
$val = $val + $min;
}
$current = $val;
$final .= chr($current);
}
return $final;
}
echo 'Test 123 (4) -> '.rotn("Test 123", 4).'<br>'; echo 'Test 123 (8) -> '.rotn("Test 123", 8).'<br>'; echo 'Test 123 (-4) -> '.rotn("Test 123", -4).'<br>'; $value = rotn("Test 123", 8);
echo 'Test 123 (Reverse [8 then -8]) -> '.rotn($value, -8).'<br>'; ?>