phptutorials

PHP code to reverse the order of words in a string by N words at a time.

There are many post you may find reversing a string by each characters, but in this post we are going to see how to write a PHP program to reverse the entire sentence word by word (order of the word) by a given N words at a time.

Example 1:

Input: This is a short sentence
N: 1
Result: sentence short a is This

Example 2: 

Input: This is a short sentence
N: 2
Result: sentence a short This is

 

Lets see the PHP code:

Read comments in the above script to understand how it works.

You can see the demo from the below link:

demo

 

 

Related Posts

Leave a Reply