Page 1 of 1

Looping text generator code

Posted: Wed Aug 27, 2003 10:46 am
by beavis
Hi, very new to the php game and I'm currently playing around with this code (written by Snowknight) that generates random text:

<?php
//Copyright Snowknight
function NewPassword () {
$Text = "This is merely the text that will be encrypted, change it if needed";
$Length = 10; #How long the password will be
$Text = md5($Text);
srand ((double) microtime() * 1000000);
$StringLength = strlen($Text);
$Beginpoint = rand(0,($StringLength-$Length-1));
$EncPassword = substr($Text, $Beginpoint, $Length);
print ("$EncPassword");
}
?>
<html>
<head><title>Password Generator</title>
</head>
<body bgcolor="#FFFFFF">
<?php
NewPassword();
?>
</body>
</html>

I want to generate more than one unique NewPassword and the most that I've done is simply echo the NewPassword script -

<?php
NewPassword();
echo NewPassword();
?>

I'd like to repeat this script 10-100 times, or whatever I choose (I'd also like to add a <br /> properly, as all of my attempts are failing). Is echo the right choice, should I be looping it using 'while' or 'for'? The goal is to learn how to loop and stop after a set number of times. Thanks.

Posted: Wed Aug 27, 2003 10:54 am
by Nay
Use:

Code: Select all

<?
for ($i=0;$i<[b]100[/b];$i++) {
NewPassword();
}
?>
that'll run the function 100 times. Change the 100 to your number of choice.

Hope that works for you...

-Nay

Posted: Wed Aug 27, 2003 11:16 am
by JAM
Or...

Code: Select all

<?php
function NewPassword ($Times) { 
 $Text = "The text..."; 
 $Text = md5($Text);
 $Length = 10;
 for ($i=0;$i<$Times;$i++) {
   srand ((double) microtime() * 65546); 
   $StringLength = strlen($Text); 
   $Beginpoint = rand(0,($StringLength-$Length-1)); 
   $EncPassword = substr($Text, $Beginpoint, $Length); 
   echo $EncPassword . '<br />';
  }
} 
// Call the function, and add how many times you want one generated.
NewPassword(10);
?>
Sidenote:
Nay's example works, But added misc chars in this line:

Code: Select all

for ($i=0;$i&lt;&#1111;b:4afb9d1442]100&#1111;/b:4afb9d1442];$i++) &#123;
because I think he tried to make the 100 bolded.

Code: Select all

for ($i=0;$i&lt;100;$i++) &#123;

Posted: Wed Aug 27, 2003 1:12 pm
by beavis
Thanks. Jam, your code works well, but I'm surprised at the number of repeating codes. Any thoughts on this?

Posted: Wed Aug 27, 2003 1:46 pm
by JAM
Guessing... Runs to fast. Seeding the rand using microseconds might not be enough. If noone has a better solution, you might add a check if the pass generated is the same as the previous one...

Posted: Wed Aug 27, 2003 4:54 pm
by Nay
O_o

[b:4afb9d1442]100[/b:4afb9d1442];

Yeah, I tried adding the [ b ] and [/ b] to highlight out the 100 for him.

-Nay

Posted: Wed Aug 27, 2003 11:42 pm
by JAM
Nay,
I tried that multible times too. Personally I think that should be added as a hack into the phpBB's code, as it's very useful in demonstrative purposes.