nonexistentera Posted October 21, 2008 Share Posted October 21, 2008 Hello. I have the script listed below, which will insert all possible combinations of the listed symbols. <? ini_set("memory_limit","300M"); ini_set("max_execution_time","360"); 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; } ?> All that will show below is the time it took for the script to execute. If the script never finishes(based on memory or execution time) then it will display an error <br /> <? $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; $start = $time; mysql_connect(TheUsual); mysql_select_db("DB"); $ts = 3; $tt = 4; $a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*?"; while($ts < $tt){ $Array=permutations($a,$ts); for($i=0 ; $i < count($Array) ; $i++) { /* echo "$i." . $Array[$i] . "<BR>"; */ mysql_query("INSERT INTO `permutations` ( `id` , `anValue` ) VALUES ( NULL , '$Array[$i]') LIMIT 500 ") or die(mysql_error()); } $ts++; } $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; $finish = $time; $totaltim[code] e = ($finish - $start); printf ("Database Inserted in %f seconds.", $totaltime); ?> [/code] I have a problem once the length exceeds 5 characters in length. The output is so large, and takes so long, that it overdoes the CPU. I tried doing something like $a $b $c $d with each equaling 6-10 characters. Then I connect them together like $a.$b $a.$c $a.$d $b.$c $b.$d $c.$d But this would have multiple values for each length. such as AA for the first length, and AA for the next length. Is there any way to get all the possible values, without repeating, and without exceeding CPU. I completly stumped on this, even after thinking it over many times. Anyone have any ideas?? If so Im glad to hear as I don't know what else I could do. Quote Link to comment https://forums.phpfreaks.com/topic/129337-how-can-i-do-this-without-repeating-and-entry-and-not-overload-the-cpu/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.