Home  >  Article  >  Backend Development  >  How to sort strings in reverse direction in PHP

How to sort strings in reverse direction in PHP

青灯夜游
青灯夜游Original
2021-06-10 18:06:152133browse

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.

How to sort strings in reverse direction in PHP

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 = &#39;hello world!&#39;;
$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn