on Tue, 2 Dec 2003 23:46:33 +0100 I already wrote, regarding replacing macro %rand[x]-[y] with random of random length ([x]-[y]).
Before, I had my own (slow!) procedure to find next occurence of the macro and generated a new string, which was returned. This took about 15 seconds for 10.000 addresses, where the macro could appear. As this was too slow, I rewrote it to use preg_replace (what a cool thing, to use a function as replacement) and this takes it down to about 2,5 seconds!! Very great - just want to lez you know that.. ;) So this is it: // replaces %rand[x]-[y]% with random text with length between [x] and [y] function parserandstr($toparse){ function genrandstr($minlen, $maxlen){ echo $minlen, $maxlen, '<br>'; $ch = array( 'punct' => array('.', '.', '.', '..', '!', '!!', '?!'), 'sep' => array(', ', ' - '), 'vocal' => array('a', 'e', 'i', 'o', 'u'), 'cons_low' => array('x', 'y', 'z'), 'cons_norm' => array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w') ); $ch['newlineafter'] = array_merge(' ', $ch['punct'], $ch['sep']); $rlen = rand($minlen, $maxlen); // generate random string $randstr = ''; $inword = 0; $insentence = 0; $lastchar = ''; $lastwasvocal = false; $lastnewline = 0; $rbreak = rand(0, 100); for ($j = 0; $j < $rlen; $j++){ if ($inword > 3 && rand(0, 5) == 1) { // punctuation chars if (rand(0,5) > 0) $char = ' '; else { $char = $ch['punct'][rand(0, count($ch['punct'])-1)] . ' '; $j += strlen($char)-1; $insentence = 0; } $inword = 0; } else { if (!$lastwasvocal && rand(0, 10) > 6) { // vocals $char = $ch['vocal'][rand(0, count($ch['vocal'])-1)]; $lastwasvocal = true; } else { do { if (rand(0, 30) > 0) // normal priority consonants $char = $ch['cons_norm'][rand(0, count($ch['cons_norm'])-1)]; else $char = $ch['cons_low'][rand(0, count($ch['cons_low'])-1)]; } while ($char == $lastchar); $lastwasvocal = false; } $inword++; $insentence++; } if ($insentence == 1 || ($inword == 1 && rand(0, 30) < 10)) $randstr .= strtoupper($char); else $randstr .= $char; $lastchar = $char; if ($lastnewline > $rbreak && rand(0, 30) > 10 && in_array($char, $ch['newlineafter'])){ $randstr .= "\n"; $lastnewline = 0; $rbreak = rand(0, 100); } else $lastnewline++; } return $randstr; } $pattern = array('/%rand(\d+)-(\d+)%/e', '/%rand%/e'); $replace = array('genrandstr($1, $2)', 'genrandstr(5, 40)'); return preg_replace($pattern, $replace, $toparse); } -- shinE! http://www.thequod.de ICQ#152282665 PGP 8.0 key: http://thequod.de/danielhahler.asc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php