1.5.1. ProblemYou want to reverse the words or the bytes in a string. 1.5.2. SolutionUse strrev( ) to reverse by byte, as in Example 1-19. Reversing a string by byte
Example 1-19 prints: .emordnilap a ton si sihT To reverse by words, explode the string by word boundary, reverse the words, and then rejoin, as in Example 1-20. Reversing a string by word
Example 1-20 prints: turtle. a was there time a upon Once 1.5.3. DiscussionReversing a string by words can also be done all in one line with the code in Example 1-21. Concisely reversing a string by word
1.5.4. See AlsoRecipe 23.7 discusses the implications of using something other than a space character as your word boundary; documentation on strrev( ) at http://www.php.net/strrev and array_reverse( ) at http://www.php.net/array-reverse. |