Voting

: zero minus zero?
(Example: nine)

The Note You're Voting On

Chris
6 years ago
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>'; // Xiwx$567
echo 'Test 123 (8) -> '.rotn("Test 123", 8).'<br>'; // \m{|(9:;
echo 'Test 123 (-4) -> '.rotn("Test 123", -4).'<br>'; // Paop-./

$value = rotn("Test 123", 8);
echo
'Test 123 (Reverse [8 then -8]) -> '.rotn($value, -8).'<br>'; // Test 123

?>

<< Back to user notes page

To Top