Return to Snippet

Revision: 6111
at April 29, 2008 01:44 by koncept


Initial Code
/**
 * Returns a rot13 encrypted string as well as a JavaScript decoder function.
 * @param string $inputString The string to encrypt
 * @return string An encoded javascript function
 */

function js_rot13_encode($inputString) {
	$rotated = str_replace('"','\"',str_rot13($inputString));
	return <<<EOF
		 <script type="text/javascript">
		/*<![CDATA[*/
		document.write("$rotated".replace(/[a-zA-Z]/g, function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}));
		/*]]>*/
		</script>

EOF;
// N.B Make sure there are no whitespace or extra characters following the semicolon above!
}

// ========
// = Demo =
// ========

echo js_rot13_encode('<a href="mailto:[email protected]">[email protected]</a>');
echo js_rot13_encode('[email protected]');

Initial URL


Initial Description
This function was respectfully borrowed from TextMate and ported to PHP. Thanks to Allan Odgaard for the function! ;)

Obviously, this can be extended a lot more, but I wanted to keep it as simple as possible for the example.

Initial Title
PHP JavaScript ROT13 Encoder Function

Initial Tags
javascript, php, function

Initial Language
PHP