How do I use this code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kakarot vegeta
    New Member
    • Jan 2011
    • 2

    How do I use this code?

    I found a code to make a list of possible permutations. How do I use this code?
    Permutation Generator.I am a newbie to php.

    Code:
    <?
    function permutations($letters,$num){
    $last = str_repeat($letters{0},$num);
    $result = array();
    while($last != str_repeat(lastchar($letters),$num)){
    $result[] = $last;
    $last = char_add($letters,$last,$num-1);
    }
    $result[] = $last;
    return $result;
    }
    function char_add($digits,$string,$char){
    if($string{$char} <> lastchar($digits)){
    $string{$char} = $digits{strpos($digits,$string{$char})+1…
    return $string;
    }else{
    $string = changeall($string,$digits{0},$char);
    return char_add($digits,$string,$char-1);
    }
    }
    function lastchar($string){
    return $string{strlen($string)-1};
    }
    function changeall($string,$char,$start = 0,$end = 0){
    if($end == 0) $end = strlen($string)-1;
    for($i=$start;$i<=$end;$i++){
    $string{$i} = $char;
    }
    return $string;
    }
    ?>
    To use this Generator you can do something like this :

    Code:
    <?
    $Array=permutations("ABC",3);
    for($i=0 ; $i < count($Array) ; $i++) {
    echo "$i." . $Array[$i] . "<BR>";
    }
    ?>
    Last edited by Dormilich; Jan 15 '11, 08:11 PM. Reason: please use [code] [/code] tags when posting code
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    How?

    Read your own post. Specifically the code just after this sentence:

    "To use this Generator you can do something like this"

    And that's an example of how you use it.


    Dan

    Comment

    Working...