Who knows how to do this...lol...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

Who knows how to do this...lol...

Post by jclarkkent2003 »

Hi,
I ran into another problem just now.

I need to build a small php script that can take a phrase or 2-3 words or more, and find every re arrangement:

example:

user enters:

php - it just outputs php
php dev - it outputs "php dev" and "dev php"
php dev network - it outputs "php dev network" and "dev php network" and " network dev php" and all the other of the 9 possibilities.

I'd like this to go up to like 9 MAXimum.

Who has a script I can look at and modifiy a function?

I'm sure this has to do with like two or more loops but I can't quite code it even though it's so simple.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

There was a discussion reagarding this here. Most of the BBCodes aren't proper though - I just edited them.
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

Post by jclarkkent2003 »

thanks, that should do exactly what I was looking for, just gotta slightly modifiy :)

God I love this forum~!

EDIT: Except now I see it's char by char, gotta modifiy a little more than slightly to store each word in an array like this , $array = explode(" ",$string) , then modifiy so it uses the array.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

shouldn't be too hard to adapt it.. As soon as the applied algorithm becomes clear :)

recursion:
- When there is only one element, simply return that element (only possibility)
- Otherwise: Place each character in front, and then make all possible permutations with the remaining
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

:)
Post Reply