Home > Article > Backend Development > How to sort strings in reverse direction in PHP
Method: 1. Use the strrev() function to reverse the string. 2. First use str_split() to split the string into an array, then use array_reverse() to reverse the order of the elements in the array, create a new array and return it; finally use the implode() function to convert the new array into a string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Method 1: Use strrev() Function
strrev() function reverses a string and returns the reversed string.
Example:
<?php echo strrev("Hello World!"); ?>
Output:
!dlroW olleH
Method 2: Using str_split() array_reverse() implode() function
First use the str_split() function to split the string into an array, then use the array_reverse() function to reverse the order of the elements in the original array, create a new array and return it; finally use the implode() function to convert the new array is a string.
<?php $str = 'hello world!'; $arr1 = str_split($str); $arr2 = array_reverse($arr1); $str = implode($arr2); echo $str; ?>
Output
!dlroW olleH
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to sort strings in reverse direction in PHP. For more information, please follow other related articles on the PHP Chinese website!