> suppose there's a string
> $string="I like my(hot) coffee with sugar and (milk)(PHP)";
>
> I would like to get an output of all possible combinations of the sentence
> with the words between brackets:
> eg.
> I like my hot coffee with sugar and
> I like my hot coffee with sugar and milk
> I like my hot coffee with sugar and milk php
> I like my coffee with sugar and
> I like my coffee with sugar and milk php
> etc.etc.
>
> The number of the words between brackets can differ and they can occur
> everywhere. The most important is that all combinations are returned but
the
> sentence (not between brackets) should be there.
>
> I got something like:
> preg_match("/\((.*?)\)(.*)\((.*?)\)+/",$string,$regresult);
>
>
> but the \((.*?)\) part that matches the things between brackets is fixed.
> There can be a variable number of these words between brackets. How do i
do
> this? And how could i easily get the combinations? Can i do it all just
with
> preg_match or should i construct the combinations with some additional
code?

I'm just thinking outloud here. I'll see if I can get some time to write
actual code later.

1. match all words between paranthesis and place into $match.

2. replace words with placeholders, something like <%1%>, <%2%>, etc...

3. count how many words you matched (assume 3 for this example).

4. Now, i'm thinking of making all of your sentences sort of like a binary
number. Since we have three matches, we have a 3 digit binary number. To
diplay all of the words in all of the combinations, we have to count from
000 to 111 where 0 is the word not showing up and one is it showing up.

000 => I like my coffee with sugar and
001 => I like my coffee with sugar and PHP
010 => I like my coffee with sugar and milk

101 => I like myhot coffee with sugar and PHP
etc...

5. So if you have $z matches, you'll want to loop from $x = 0 until
$x=bindec(str_repeat("1",$z))

6 make a copy of $x... $copy = $x.

7 While ($copy != 0)

8. if($copy & 1), then replace placeholder <%$x%> with $match[$x]

9. Shift the bits in $copy one to the right. ($copy>>1) and go back to #7.

10. Once $copy is zero, replace any placeholders left with an empty string
and display the sentence.

11. Go back to #5 and increment x (next for loop).

I hope that's confusing enough... or not confusing.. either way. I can write
some actual code if you want, but it'll be later today after work. Let me
know.

---John Holmes...



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to