W3docs

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()

<?php
$string = "Hello, World!";
$encoded_string = str_rot13($string);
echo $encoded_string; // Output: Uryyb, Jbeyq!
?>

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.

The str_rot13() function is a useful tool for simple obfuscation or basic puzzles, though it should never be used to protect sensitive information as it is trivially reversible.

Practice

Practice

What does the str_rot13() function do in PHP?