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

How to sort strings in reverse direction in PHP

青灯夜游
Release: 2023-03-10 14:22:01
Original
2186 people have browsed it

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!");
?>
Copy after login

Output:

!dlroW olleH
Copy after login

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;
?>
Copy after login

Output

!dlrow olleh
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template