Word Permutator Function

Give this function a phrase and it will return all of the permutations of the words that make up the phrase.

function wordperms($phrase,$top='') {
        $output = array();
       
        if (is_array($phrase)) {
                $phrase_pieces = $phrase;
        }
        else {
                $phrase_pieces = explode(' ',$phrase);
        }
        $top_pieces = explode(' ',$top);
        foreach ($phrase_pieces as $piece) {
                if (!in_array($top.$piece,$output) && !in_array($piece,$top_pieces)) {
                        $output[] = $top.$piece;
                        $output = array_merge($output,wordperms($phrase_pieces,$top.$piece.' '));
                }
        }
        return $output;
}

// example
print_r(wordperms("three word phrase"));
/*
Array
(
    [0] => three
    [1] => three word
    [2] => three word phrase
    [3] => three phrase
    [4] => three phrase word
    [5] => word
    [6] => word three
    [7] => word three phrase
    [8] => word phrase
    [9] => word phrase three
    [10] => phrase
    [11] => phrase three
    [12] => phrase three word
    [13] => phrase word
    [14] => phrase word three
)
*/

Reply

  • Allowed HTML tags: <b> <br> <p> <a> <strong> <cite> <em> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may use [img:xx] tags to display uploaded files or images inline.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options