Home  >  Article  >  Backend Development  >  How to reverse words in php string

How to reverse words in php string

*文
*文Original
2017-12-26 09:51:221239browse

How to reverse a php string by word? This article mainly introduces the method of reversing PHP strings according to words, involving the usage skills of array_reverse, explode, implode and other functions in PHP. I hope it will be helpful for everyone to reverse strings.

The specific analysis is as follows:

The following php code can reverse and output the string according to words. In fact, the string is separated into arrays according to spaces, and then the array is reversed and output.

<?php
$s = "Reversing a string by word";
// break the string up into words
$words = explode(&#39; &#39;,$s);
// reverse the array of words
$words = array_reverse($words);
// rebuild the string
$s = implode(&#39; &#39;,$words);
print $s;
?>

The output results are as follows:
word by string a Reversing

Related recommendations:

Detailed introduction to php string functions

php string function tutorial and example code

php Introduction to string segmentation and comparison

The above is the detailed content of How to reverse words in php string. 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