PHP script to output all combinations of spintax ?

BNR34

RB26DETT
Feb 16, 2011
236
1
0
Blighty
Anyone know of a script (pref php) that will take a spintax sentence and output all combinations possible?
 


A little spin on the spin function I use. Not the sexiest. Sort of brute force, but it'll work. If someone wants to improve on it go for it.

Code:
function str_replace_count($search, $replace, $subject, $times) {
    $subject_original = $subject;
    $len = strlen($search);
    $pos = 0;
    for ($i = 1; $i <= $times; $i++) {
        $pos = strpos($subject, $search, $pos);
        if ($pos !== false) {
            $subject = substr($subject_original, 0, $pos);
            $subject.=$replace;
            $subject.=substr($subject_original, $pos + $len);
            $subject_original = $subject;
        } else {
            break;
        }
    }
    return $subject;
}

function spinTextAll($spintax) {
    $results[] = $spintax;
    while (preg_match('/{([^{}]+)}/', $spintax, $matches)) { //find a match
        $parts = explode('|', $matches[1]); //break up all the possible values
        $spintax = str_replace_count($matches[0], $parts[mt_rand(0, count($parts) - 1)], $spintax, 1); //randomly replace it in the master string so that we don't keep matching the same element
        foreach ($parts as $part) { // loop through possible values
            foreach ($results as $result) {  //loop through our strings we are building
                if (strstr($result, $matches[0])) { //only if there is a matching element do a replace
                    $results[] = str_replace_count($matches[0], $part, $result, 1); // replace in the strings we are building
                }
            }
        }

    }

    $i = 0;
    foreach ($results as $result) { //loop though our results
        if (!preg_match('/{([^{}]+)}/', $result, $matches)) { //only count values that are complete
            $i++;
            echo $i . ' ' . $result . '<br />';
        }
    }
}

$spintax = 'this {is|mightbe} some {sort|kind} {of|if} {syntax|code}';
spinTextAll($spintax);
Output:
Code:
1 this is some sort of syntax
2 this mightbe some sort of syntax
3 this is some kind of syntax
4 this mightbe some kind of syntax
5 this is some sort if syntax
6 this mightbe some sort if syntax
7 this is some kind if syntax
8 this mightbe some kind if syntax
9 this is some sort of code
10 this mightbe some sort of code
11 this is some kind of code
12 this mightbe some kind of code
13 this is some sort if code
14 this mightbe some sort if code
15 this is some kind if code
16 this mightbe some kind if code