PHP str_rot13 is an inbuilt string function in PHP. It performs ROT13 transformation on a string by excluding numeric and non-alphanumeric characters. Also, if you input an encoded string, the function returns the original string back. In this article, we will discuss the PHP str_rot13 Function. Also, we will discuss a few examples of using it.
Note: The ROT13 transformation shifts every letter by 13 places in the alphabet. However, it does not change any numeric or non-alphanumeric character. Also, the same function performs encoding and decoding. Therefore, on passing the already encoded string will return the original string.
The PHP str_rot13 function expects only one parameter. The string which you want to encode/decode is the input to the function.
The str_rot13 function in PHP returns the string after applying ROT13 transformation.
Let’s discuss a few examples of using the function.
Example 1: Encoding a String
For instance, consider an example of ROT13 encoding a string using the function.
<?php $string = 'Bringing Knowledge Together'; $encoded = str_rot13($string); echo $encoded; ?>
The final string after rotating all the alphabets by 13 characters is:
Oevatvat Xabjyrqtr Gbtrgure
Example 2: Decoding a String
Similarly, we can pass the encoded string to the function to obtain the original string again.
<?php $string = 'Oevatvat Xabjyrqtr Gbtrgure'; $decoded = str_rot13($string); echo $decoded; ?>
OUTPUT: Bringing Knowledge Together
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.