How to flip the words in a sentence backwards with PHP

How do you flip the words in a sentence backwards with PHP?

This is one of those tests programmers like to give in interviews, at first it seems like you could just use the strrev() function, but it actually reverses the characters in a string which makes the words all show up backwards and so would only work for sentences made of palindromes. The actual answer is to blow the string up into an array of words using the explode() function, then reverse the order of the array with the array_reverse function, then use the implode()  function to glue the array back into a string.

The code looks like this:

I hope this is useful to you. Good luck in that interview.

Date
Categories
Tags
Permalink
Status

Published:March 2, 2016

Uncategorized

Bookmark the permalink

Post a comment or leave a trackback: Trackback URL


Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

*
*


Notice: Use of undefined constant WP_PB_URL_AUTHOR - assumed 'WP_PB_URL_AUTHOR' in /app/wp-content/plugins/adsense-box/includes/widget.php on line 164

Notice: Use of undefined constant WP_PB_URL_AUTHOR - assumed 'WP_PB_URL_AUTHOR' in /app/wp-content/plugins/adsense-box/includes/widget.php on line 164

Notice: Use of undefined constant WP_PB_URL_AUTHOR - assumed 'WP_PB_URL_AUTHOR' in /app/wp-content/plugins/adsense-box/includes/widget.php on line 164
Please wait...

Subscribe

Want to be notified when a new article is published? Enter your email address and name below to be the first to know.