Home > Article > Backend Development > How to reverse words in php string
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(' ',$s); // reverse the array of words $words = array_reverse($words); // rebuild the string $s = implode(' ',$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!