note 76975 deleted from function.str-rot13 by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 08:00:18 +0000 |
| Subject: | note 76975 deleted from function.str-rot13 by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196712@lists.php.net to get a copy of this message | ||
Note Submitter: nick at lazy-river dot net
----
This is recursive function to shift the component letters of a string left or right in the ascii
table.
I've left it simple as it suits my needs, but you may want to include error checking for a null
string and also put bounds in place, or make it actually rotate around the whole character set
rather than just shifting the string up or down.
function asc_shift($string, $amount) {
$key = substr($string, 0, 1);
if(strlen($string)==1) {
return chr(ord($key) + $amount);
} else {
return chr(ord($key) + $amount) . asc_shift(substr($string, 1, strlen($string)-1), $amount);
}
}
For example:
<?php
echo asc_shift("TESTING12345@", 5);
?>
shifts every character up 5 ascii positions, resulting in this string:
YJXYNSL6789:E
In reverse:
<?php
echo asc_shift("YJXYNSL6789:E", -5);
?>
shifts every character down 5 ascii positions, resulting in this string:
TESTING12345@