Reversing a string in PHP refers to rearranging a given string's characters in reverse order, starting from the last character to the first. This task is often used in text manipulation or algorithm challenges, highlighting PHP's string-handling capabilities.
Examples:
Input : GeeksforGeeks
Output : skeeGrofskeeG
Input : 12485
Output : 58421
Below we have discussed about three basic and most commonly used methods of reversing strings in PHP:
Reversing string using strrev()
The strrev() function in PHP provides a straightforward approach to reversing a string. It takes a string as input and returns a new string with the characters in reverse order, making it an efficient tool for text manipulation.
Syntax
strrev($string)
Example :
In this example we defines a Reverse() function that uses strrev() to reverse a given string. The string "GeeksforGeeks" is passed to the function and the reversed result is printed.
PHP
<?php
// PHP program to reverse a string using strrev()
function Reverse($str){
return strrev($str);
}
// Driver Code
$str = "GeeksforGeeks";
echo Reverse($str)
?>
Reversing string using recursion and substr()
Reversing a string using recursion and substr() involves breaking the string into smaller parts. The substr() function extracts the last character, and recursion reverses the remaining string until it reaches the base case of an empty string, then reassembles it in reverse order.
Example: In this example we defines a recursive function Reverse() that reverses a string by slicing the first character and appending it to the reversed substring. The base case returns the string when only one character remains.
PHP
<?php
// PHP function to reverse a string using
// recursion and substr()
function Reverse($str){
// strlen() used to calculate the
// length of the string
$len = strlen($str);
// Base case for recursion
if($len == 1){
return $str;
}
else{
$len--;
// extract first character and concatenate
// at end of string returned from recursive
// call on remaining string
return Reverse(substr($str,1, $len))
. substr($str, 0, 1);
}
}
// Driver Code
$str = "GeeksforGeeks";
print_r(Reverse($str));
?>
In-place reversing a string without using library functions:
In-place string reversal modifies the original string without creating a copy. The approach involves swapping characters from both ends, starting with the first and last, and continuing inward until the middle of the string is reached.
Example: This PHP function reverses a string in-place by swapping characters from both ends towards the middle, without using library functions. It iterates over the string, exchanging corresponding characters, and returns the reversed string.
PHP
<?php
// PHP function to in place reverse a string
// without using library functions
function Reverse($str){
for($i=strlen($str)-1, $j=0; $j<$i; $i--, $j++)
{
$temp = $str[$i];
$str[$i] = $str[$j];
$str[$j] = $temp;
}
return $str;
}
// Driver Code
$str = "GeeksforGeeks";
print_r(Reverse($str));
?>
Similar Reads
How to replace String in PHP ? Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each
2 min read
PHP String Functions Complete Reference Strings are a collection of characters. For example, 'G' is the character and 'GeeksforGeeks' is the string. Installation: These functions are not required any installation. These are the part of PHP core. The complete list of PHP string functions are given below: Example: This program helps us to c
6 min read
PHP String Functions Strings are a fundamental data type in PHP, used to store and manipulate text. PHP provides a wide variety of built-in string functions. These functions perform various operations such as string transformations, character manipulations, encoding and decoding, and formatting, making string handling s
6 min read
Explain some string functions of PHP In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols. For example, "Hello World!", "ID-34#90" etc. PHP also allows single quotes(' ') for defining a string. Every pro
7 min read
PHP | is_string() Function The is_string() function is an inbuilt function in PHP which is used to check whether the given value is a string or not. Syntax: bool is_string( mixed $var ) Parameters: This function accepts one parameter as mentioned above and described below: $var: It contains the value that need to check. Retur
1 min read