Login   Register  
PHP Classes
elePHPant
Icontem

File: permutate.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of BasicA  >  String Permutation Class  >  permutate.php  >  Download  
File: permutate.php
Role: ???
Content type: text/plain
Description: String Permutation
Class: String Permutation Class
Author: By
Last change:
Date: 2001-03-26 09:52
Size: 1,654 bytes
 

Contents

Class file image Download
<?php

        /* 
           basica@k-designs.com.sg 
           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>";
	}      
		


?>