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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 :)
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.


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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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