str_rot13()
Our article is about the PHP function str_rot13(), which is used to perform the ROT13 encoding on a string. The ROT13 encoding is a simple letter substitution
The str_rot13() function in PHP performs the ROT13 encoding on a string. ROT13 is a simple letter substitution cipher that replaces each alphabetic character with the letter 13 positions ahead of it in the alphabet. Non-alphabetic characters remain unchanged.
The syntax of the str_rot13() function is as follows:
The PHP syntax of the str_rot13()
str_rot13($string)The function takes one required parameter: $string. $string is the string to encode using ROT13. Note that str_rot13() only processes ASCII letters and ignores UTF-8 multibyte characters. Only alphabetic characters are transformed; all other characters remain unchanged.
Here is an example of how to use the str_rot13() function:
Example of PHP str_rot13()
In this example, we have a string variable $string that contains the phrase "Hello, World!". We use the str_rot13() function to perform the ROT13 encoding on the string by passing the variable $string as the parameter.
The output of this code will be:
Uryyb, Jbeyq!As you can see, the str_rot13() function has successfully encoded the original string using ROT13.
ROT13 is its own inverse
The most useful property of ROT13 is that encoding and decoding are the same operation. Because the alphabet has 26 letters and ROT13 shifts by exactly half of that, applying the function twice rotates a letter a full 13 + 13 = 26 positions, returning it to where it started. This means you call str_rot13() again to get the original string back — there is no separate str_unrot13() function.
<?php
$plain = "Hello, World!";
$encoded = str_rot13($plain); // Uryyb, Jbeyq!
$decoded = str_rot13($encoded); // Hello, World!
echo $encoded . "\n";
echo $decoded . "\n";
?>The output of this code will be:
Uryyb, Jbeyq!
Hello, World!What gets transformed
Only the 52 ASCII letters (a–z and A–Z) are rotated. Case is preserved, and every other character — digits, punctuation, spaces, and multibyte UTF-8 characters — passes through untouched.
<?php
echo str_rot13("PHP 7.4 — café") . "\n";
?>The output of this code will be:
CUC 7.4 — pnséNotice that 7.4, the spaces, and the em dash are unchanged, the accented é is left alone (it is a multibyte character, not ASCII), while the ASCII letters are each shifted 13 places: P→C, H→U, c→p, a→n, f→s.
When to use it
ROT13 is handy for hiding text from a casual glance — spoiler tags in forums, puzzle answers, or lightly obfuscating an email address in source code. Because it preserves length and is reversible with a single call, it is also a common teaching example of a substitution cipher.
It must never be used to protect sensitive information: ROT13 has no key and is trivially reversed by anyone. For real protection, use PHP's password_hash() for passwords or the Sodium / OpenSSL extensions for encryption.
Related functions
strrev()— reverse the characters of a string.str_replace()— substitute substrings within a string.substr_replace()— replace a portion of a string by position.