Solved

Splitting out all phrases in a string of words

Posted on 2011-09-11
6
288 Views
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
one
two three four
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!

Cheers
David
0
Comment
Question by:dolythgoe
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
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.
<?php

$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>';
var_dump($results);
echo '</pre>';

Open in new window


HTH
0
 
LVL 109

Expert Comment

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

one two two three


Thanks, ~Ray
0
 

Author Comment

by:dolythgoe
ID: 36519810
That's just brilliant - thanks so much :)
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:dolythgoe
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
0
 
LVL 10

Expert Comment

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

Author Comment

by:dolythgoe
ID: 36519823
Opps trying to accept my own question!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 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