Posted on Leave a comment

How to flip the words in a sentence backwards with PHP


( ! ) Notice: A non well formed numeric value encountered in /var/www/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118
Call Stack
#TimeMemoryFunctionLocation
10.0001396200{main}( ).../index.php:0
20.0002396488require( '/var/www/html/wp-blog-header.php' ).../index.php:17
31.609912481200require_once( '/var/www/html/wp-includes/template-loader.php' ).../wp-blog-header.php:19
41.617712534176include( '/var/www/html/wp-content/themes/storefront/single.php' ).../template-loader.php:78
51.763613091704get_template_part( ).../single.php:19
61.763613092224locate_template( ).../general-template.php:168
71.763713092320load_template( ).../template.php:671
81.763713092592require( '/var/www/html/wp-content/themes/storefront/content-single.php' ).../template.php:724
91.764313093344do_action( ).../content-single.php:21
101.764413093720WP_Hook->do_action( ).../plugin.php:465
111.764413093720WP_Hook->apply_filters( ).../class-wp-hook.php:310
121.767013097136storefront_post_content( ).../class-wp-hook.php:286
131.767113098208the_content( ).../storefront-template-functions.php:414
141.767213099104apply_filters( ).../post-template.php:247
151.767213099504WP_Hook->apply_filters( ).../plugin.php:208
161.774713173552CrayonWP::the_content( ).../class-wp-hook.php:286
171.779013236320CrayonHighlighter->output( ).../crayon_wp.class.php:703
181.779913240384CrayonFormatter::print_code( ).../crayon_highlighter.class.php:189

( ! ) Notice: A non well formed numeric value encountered in /var/www/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119
Call Stack
#TimeMemoryFunctionLocation
10.0001396200{main}( ).../index.php:0
20.0002396488require( '/var/www/html/wp-blog-header.php' ).../index.php:17
31.609912481200require_once( '/var/www/html/wp-includes/template-loader.php' ).../wp-blog-header.php:19
41.617712534176include( '/var/www/html/wp-content/themes/storefront/single.php' ).../template-loader.php:78
51.763613091704get_template_part( ).../single.php:19
61.763613092224locate_template( ).../general-template.php:168
71.763713092320load_template( ).../template.php:671
81.763713092592require( '/var/www/html/wp-content/themes/storefront/content-single.php' ).../template.php:724
91.764313093344do_action( ).../content-single.php:21
101.764413093720WP_Hook->do_action( ).../plugin.php:465
111.764413093720WP_Hook->apply_filters( ).../class-wp-hook.php:310
121.767013097136storefront_post_content( ).../class-wp-hook.php:286
131.767113098208the_content( ).../storefront-template-functions.php:414
141.767213099104apply_filters( ).../post-template.php:247
151.767213099504WP_Hook->apply_filters( ).../plugin.php:208
161.774713173552CrayonWP::the_content( ).../class-wp-hook.php:286
171.779013236320CrayonHighlighter->output( ).../crayon_wp.class.php:703
181.779913240384CrayonFormatter::print_code( ).../crayon_highlighter.class.php:189

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.