Random String Generation
Posted: 28 November 2008 04:05 AM   [ Ignore ]  
Summer Student
Total Posts:  18
Joined  10-04-2007

Hi all,

I am trying to use a script to randomly generate a string for a validation code.  When I put it into my template, I get an error.

Here is the code that I am using:

<?php
function rand_text(   $min = 10,
                      
$max = 20,
                      
$randtext = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' )
{
    
if($min < 1) $min=1;
    
$varlen = rand($min,$max);
    
$randtextlen = strlen($randtext);
    
$text = '';

    for(
$i=0; $i > $varlen; $i++)
    
{
        $text
.= substr($randtext, rand(1, $randtextlen), 1);
    
}
    
return $text;
}
?>

And to call the function:

<?php echo rand_text(); ?>

The error I get is this:

Parse error: syntax error, unexpected ';' in Y:\MyDirectory\system\core\core.functions.php(637) : eval()'d code on line 8

I can’t see why it’s not working…any help on this one is appreciated.  Perhaps there is also a better way to randomly generate a string in EE??

Profile
 
 
Posted: 28 November 2008 10:11 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  66
Joined  09-08-2002

For starters I cut and pasted your function into a test template but didn’t get an error or output.  The reason for no output is that your for loop never iterates.  It should be

for ($i=1; $<=$varlen; $i++)  not for($i=0; $>$varlen;$i++)

With that change it generates a random string of 10 to 20 characters and no errors.

Herb

 Signature 

Everybody is entitled to my opinion!

Profile
 
 
Posted: 29 November 2008 04:03 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  18
Joined  10-04-2007

Thanks Herb.

Still no joy.  I have tried with the below:

<?php
function rand_text(   $min = 10,
                      
$max = 20,
                      
$randtext = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' )
{
    
if($min < 1) $min=1;
    
$varlen = rand($min,$max);
    
$randtextlen = strlen($randtext);
    
$text = '';

    for (
$i=1; $<=$varlen; $i++)
    
{
        $text
.= substr($randtext, rand(1, $randtextlen), 1);
    
}
    
return $text;
}
?>

<?php
echo rand_text(); ?>

Which gives me this new error:

Parse error: syntax error, unexpected T_IS_SMALLER_OR_EQUAL, expecting T_VARIABLE or '$' in Y:\MyDirectory\system\core\core.functions.php(637) : eval()'d code on line 11

Any ideas?

Profile
 
 
Posted: 29 November 2008 04:07 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  18
Joined  10-04-2007

Ahhh spotted it, the code should be:

for ($i=1; $i<=$varlen; $i++)

Thanks for your help smile

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 66388 Total Logged-in Users: 40
Total Topics: 84713 Total Anonymous Users: 18
Total Replies: 454686 Total Guests: 192
Total Posts: 539399    
Members ( View Memberlist )