Random Characters: Still can't get it to work Chr(rand());

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post by tomprogers »

Not quite. I substituted letters because I didn't want to bother to refer to the ranges you specified.

You have established that you are interested in certain ASCII values, like 65-72.

First, you create an array, each element of which represents the ASCII value of a character that is acceptable.
E.g. $chars = array(61, 62, 63);


Because you want a set of non-continguous spans, you will use the range() function to save some time.
E.g. $chars_1 = range(61, 63); $chars_2 = range(65, 68);

You will mash all these little spans together, to create the complete array.
E.g. $chars = array_merge(range(61, 63), range(65, 68));

Then you will convert the raw ASCII codes into the actual characters they represent, using array_map() to apply the chr() function to each element.
$chars = array_map('chr', $chars);

Finally, you will condense the array of characters into a single long string, using implode().
E.g. $pool = implode('', $chars);

Note that you do not pass the actual characters you want to the range() function. You can, but remember that later you apply the chr() function to each item. I'm sure you don't want the result of chr(A) - you want a friggin' A. Because you intend to do this, you create ranges of the ASCII codes only.

I also suggest that you try out the code you're curious about, rather than simply asking if it's right in the forum. You will never get any work done if you have to wait for someone else to volunteer to review it.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Ok, I posted something in the Volunteer Work section. I have this now:

Code: Select all

<?php 

        $code = ''; 
        $chars = array(50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 80, , 82, 83, 84, 85, 86, 87, 88,89, 90); 
		$chars = array_map('chr', $chars); 
        $pool = implode('', $chars); 
		$size = strlen($pool) - 1; 
        for($i = 0; $i < $len; ++$i) 
        { 
          $code .= $pool[mt_rand(0, $size)]; 
        } 
                return $code; 

?>
Is it right? <---- I keep asking this because I need this code to be right before I post it to my site, because I get a lot of users using the page with it. And if it goes down the users would be :( .
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post by tomprogers »

That's close enough. But keep in mind that these two are equivalent:

Code: Select all

$the_long_way = array(50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 77, 78, 80, , 82, 83, 84, 85, 86, 87, 88,89, 90); 
$the_short_way = array_merge(range(50, 57), range(65, 78), array(80), range(82, 90));

$example = range(1, 10) == array(1,2,3,4,5,6,7,8,9,10);
Post Reply