In PHP, we can perform an ROT13 transformation of a string using the str_rot13
function.
ROT13 is an algorithm that replaces a letter with the 13th letter after it in the alphabet.
Do not use this as an encryption algorithm.
str_rot13(string $string): string
This function requires the string we want to encode as a mandatory parameter.
The str_rot13
function will return the computed ROT13 of the given string.
This function works as both a decoder and an encoder.
This function will only encode the alphabet characters; it leaves all other characters untouched.
In this example, we will encode the string and then decode it back to the original version.
<?php$mystr = "Hello, Educative!";$mystr = str_rot13($mystr);echo "First encode: " . $mystr . "\n"; // note that the "!" is untouched!$mystr = str_rot13($mystr);echo "Second encode: " . $mystr;
str_rot13
function is applied to the input string.str_rot13
function is applied to the encoded string to be decoded.