Location: PHPKode > scripts > String Permutation Class > string-permutation-class/permutate.php
<?php

        /* 
           hide@address.com 
           http://staff.k-designs.com.sg/basica/
 
         */


	function Permutate($strDataIn, $Length, &$PermutateCount) {
	
		for ($i = 0; $i < strlen($strDataIn); $i++) {
			$PermArray[0][$i] = substr($strDataIn, $i, 1);
			$temp[$i]         = substr($strDataIn, $i, 1);
			$temp2[0][$i]     = substr($strDataIn, $i, 1);  
		}                                 
		
		/* for ($i = 1; $i < strLen($strDataIn); $i++) { */
		for ($i = 1; $i < $Length; $i++) {
			for ($k = 0; $k < strLen($strDataIn); $k++) {
				for ($j = 0; $j < sizeof($temp2[$i - 1]); $j++) {
					$PermArray[$i][($k * sizeof($temp2[$i - 1])) + $j] = $temp[$k] . $temp2[$i - 1][$j];
					$temp2[$i][($k * sizeof($temp2[$i - 1])) + $j]     = $temp[$k] . $temp2[$i - 1][$j];
				}		
			}
		}   
		
		$k = 0;

		/* for ($i = 0; $i < strlen($strDataIn); $i++) { */

		for ($i = 0; $i < $Length; $i++) {
			$k += sizeof($PermArray[$i]);
		}
		
		$PermutateCount = $k;
		
		return $PermArray;		      

/*		for ($i = 0; $i < $Length; $i++) {
			for ($j = 0; $j < sizeof($PermArray[$i]); $j++) {
				print $PermArray[$i][$j] . "<br>";
			}	                                
			print "<br>";
		}      
*/		
	}                  
	
	$StartString = "abcde"; 
	$len         = 5;  
	
	$Return = Permutate($StartString, $len, $cnt);
	
	print "Returned <b>$cnt</b> permutations.<br><hr>";
	
	for ($i = 0; $i < $len; $i++) {
		for ($j = 0; $j < sizeof($Return[$i]); $j++) {
			print $Return[$i][$j] . "<br>";
		}	                                
		print "<br>";
	}      
		


?>


Return current item: String Permutation Class