monkeypaw201 Posted March 19, 2009 Share Posted March 19, 2009 Alright, so i have the code below, and i've run it up to (and including) 3 Characters, but after that it wants over 256MB or Memory (hosting provider won't allocate more than that). Is there any way to break it up into smaller chunks? [code<?php $chars = array("A","a","B","b","C","c","D","d","E","e","F","f","G","g","H","h","I","i","J","j","K","k","L","l","M","m","N","n","O","o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v","W","w","X","x","Y","y","Z","z"," ","`","~","1","!","2","@","3","#","4","$","5","%","6","^","7","&","8","*","9","(","0",")","-","_","=","+","\"","|","}","{","]","[",";",":","?","/",".",">","<"); function comb($a, $len){ if ($len > count($a))return 'error'; $out = array(); if ($len==1) { foreach ($a as $v) $out[] = array($v); return $out; } $len--; while (count($a) > $len) { $b = array_shift($a); $c = comb($a, $len); foreach ($c as $v){ array_unshift($v, $b); $out[] = $v; } } return $out; } $test = $chars; $a = comb($test,$_GET['number']); $count = 0; foreach($a as $arr) { //print_r($arr); $string = ""; $count++; foreach($arr as $outrr) { $string .= $outrr; //echo "<br>"; //echo "-"; } //echo $string; //echo "-"; //echo "<br>"; //echo "<hr>"; mysql_query("INSERT INTO `sha1` (`string`) VALUES ('$string')"); } echo $count; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150148-how-can-i-break-up-a-large-foreach/ Share on other sites More sharing options...
kenrbnsn Posted March 19, 2009 Share Posted March 19, 2009 What are you trying to accomplish with this code? Ken Quote Link to comment https://forums.phpfreaks.com/topic/150148-how-can-i-break-up-a-large-foreach/#findComment-788532 Share on other sites More sharing options...
monkeypaw201 Posted March 19, 2009 Author Share Posted March 19, 2009 Generate every possible combination of the $chars with a defined length. ie AAAA, AAAa, AAAB, AAAb, AAAC, AAAc, etc.. ie AAA, AAa, AAB, AAb, AAC, AAc, etc.. Quote Link to comment https://forums.phpfreaks.com/topic/150148-how-can-i-break-up-a-large-foreach/#findComment-788536 Share on other sites More sharing options...
sasa Posted March 19, 2009 Share Posted March 19, 2009 try <?php $chars = array("A","a","B","b","C","c","D","d","E","e","F","f","G","g","H","h","I","i","J","j","K","k","L","l","M","m","N","n","O","o","P","p","Q","q","R","r","S","s","T","t","U","u","V","v","W","w","X","x","Y","y","Z","z"," ","`","~","1","!","2","@","3","#","4","$","5","%","6","^","7","&","8","*","9","(","0",")","-","_","=","+","\"","|","}","{","]","[",";",":","?","/",".",">","<"); $total_number = 1; $char_num = count($chars); $len = $_GET['number']; //$len =2; for ($i=0; $i<$len;$i++) $total_number *= $char_num; for ($i=0; $i<$total_number; $i++){ $a = $i; $out = ''; for ($j=0;$j<$len;$j++){ $out = $chars[$a % $char_num].$out; $a = (int) ($a/$char_num); } // echo $out,"\n"; mysql_query("INSERT INTO `sha1` (`string`) VALUES ('$out')"); } ?> think about time for $len=2 time is in seconds if $len=3 time is in minutes $len=4 => hours for $len=5 times is in days for $len=6 times is in years etc. Quote Link to comment https://forums.phpfreaks.com/topic/150148-how-can-i-break-up-a-large-foreach/#findComment-788626 Share on other sites More sharing options...
sloth456 Posted March 19, 2009 Share Posted March 19, 2009 You use a cron to do bits of it in chunks and store the results in a database. Quote Link to comment https://forums.phpfreaks.com/topic/150148-how-can-i-break-up-a-large-foreach/#findComment-788667 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.