Splitting out all phrases in a string of words

Posted on 2011-09-11
Last Modified: 2012-05-12
Hi all,

I'm looking for some efficient php code to do the following:

Consider an input string of "one two three four" (any numer of words delimited by spaces).

I would like to output all possible combinations of phrases and words in that sentance e.g. :

one two three four
one two three
one two
two three four
three four

Would like to then put them in a string comma seperated (e.g. one two three four, one two three, one two, one, two three four,...)

Doesn't have to be in that order but any help on this much appreciated - driving me mad!

Question by:dolythgoe
  • 3
  • 2
LVL 10

Accepted Solution

Derokorian earned 250 total points
ID: 36519791
Wrote this, output what you were hoping for, added the vardump at the bottom to show what it looks like before imploding.

$test = "one two three four";
$rescount = 1;
$results = array();
$result[] = $test;

$words = explode(' ',$test);
$wordc = count($words);

for( $i=0; $i<$wordc; $i++ ) {
	for( $j=$wordc; $j>0; $j-- ) {
		$results[$rescount] = '';
		for( $k=$i; $k<$j; $k++ ) {
			$results[$rescount] .= ' '. $words[$k];
		if( !empty($results[$rescount]) ) $rescount++;
if( empty($results[$rescount]) ) unset($results[$rescount]);

echo implode(',',$results);

echo '<pre>';
echo '</pre>';

Open in new window

LVL 108

Expert Comment

by:Ray Paseur
ID: 36519809
What would you want for the output from this:

one two two three

Thanks, ~Ray

Author Comment

ID: 36519810
That's just brilliant - thanks so much :)
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Author Comment

ID: 36519818
I've requested that this question be closed as follows:

Accepted answer: 0 points for dolythgoe's comment http:/Q_27302373.html#36519810

for the following reason:

Top reply in no time at all
LVL 10

Expert Comment

ID: 36519819
If the reply works, please accept it as the solution, not your own comment as that does not award points.

Author Comment

ID: 36519823
Opps trying to accept my own question!

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now