Better Converter

Reverse String Online Tool

Enter the text to be reversed


File max size 10MB

Reversed string

Reverse String - Online Converter

PHP serves us with many built-in methods which can be used to manipulate strings.

Below three basic and most commonly used methods of reversing strings in PHP

Reversing string using strrev(): The strrev() function is a built-in function available in PHP and is used to reverse strings. This function takes a string as argument and returns a reversed string.

Reversing string using recursion and substr(): We can also reverse a string using recursion and substr() function. The substr() function is used to get a substring of the original string. Here we have defined a function Reverse() with the string passed as argument. During every recursive call, we have used the substr() method to extract the first character of argument string and called the Reverse() function again by passing remaining part of string as argument and concatenated the first character at the end of the string returned from current call.

In-place reversing a string without using library functions: In-place reversal of string means to reverse the string by doing modification in the original string itself and not making any copy of the original string. We can reverse a string in-place and without using any library function in PHP. The idea to do so is to traverse the original string from both sides, i.e. from both left and right until we reach the middle of the string. And keep swapping the characters while traversing. So, we will simply swap the characters, starting with the first and last, then second-first and second-last and so on, till we reach the middle of the string.

> source geeksforgeeks.org