News:

SMF 2.1.6 has been released! Take it for a spin! Read more.

Main Menu

Function: rand_str();

Started by Matthew K., February 04, 2012, 04:41:32 PM

Previous topic - Next topic

Matthew K.

Okay, so as some of you may know, I was writing a Mobile Notifications System for SMF that came to a halt due to some information regarding it not working well on larger servers due to traffic being passed through from email-to-text.

One of the things this modification required was a verification system, that consisted of a random string being passed to the cell-phone to verify the phone.

So I had to write a rand_str(); PHP function.

Here it is, if anyone wants to use it, I'd appreciate credits being left if utilized.
/**
* rand_str()
*
* @author: [email protected]
* @copyright: Matthew "Labradoodle-360" Kerle
*
* @param integer $length
* @param string $char_set
* @param string $case
* @return
*/
function rand_str($length = 8, $char_set = 'all', $case = 'mixed')
{

$length = (int) $length;
if (empty($length))
return false;
elseif ($length > 30)
$length = 30;

$rand_str = '';

$allowed_case_types = array(
'lower', 'upper', 'mixed'
);

if (!in_array($case, $allowed_case_types))
$case = 'mixed';

$alphabetical_characters = array(
'lowercase' => array(
'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
),
'uppercase' => array(
'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z'
)
);

$numerical_characters = array(
'1', '2', '3',
'4', '5', '6',
'7', '8', '9',
'0'
);

$special_characters = array(
'!', '@', '#', '$',
'%', '^', '&', '*',
'(', ')', '-', '_',
'+', '=', '"', '\'',
'<', ',', '>', '.',
'?', '/', '[', ']'
);

if ($case === 'lower')
$alphabetical_characters = $alphabetical_characters['lowercase'];
elseif ($case === 'upper')
$alphabetical_characters = $alphabetical_characters['uppercase'];
else
$alphabetical_characters = array_merge($alphabetical_characters['lowercase'], $alphabetical_characters['uppercase']);

switch ($char_set)
{
case 'alphabetical':
$char_set = &$alphabetical_characters;
break;
case 'numerical':
$char_set = &$numerical_characters;
break;
case 'alphanumerical':
$char_set = array_merge($alphabetical_characters, $numerical_characters);
break;
case 'special_chars':
$char_set = &$special_characters;
break;
default:
$char_set = array_merge($alphabetical_characters, $numerical_characters, $special_characters);
break;
}

$temp_arr = array_keys($char_set);
for ($i = 1; $i <= $length; $i++)
$rand_str .= $char_set[mt_rand(reset($temp_arr), end($temp_arr))];

return $rand_str;

}

JBlaze

I wrote a much simpler random string generator for use with my URL shortener. It's a bit quicker and less complex, and still gets the same job done.

<?php
/**
* Random key generation
*
* @param int $amount The string length of the key you want returned
* @return string
*/
function randkey($amount) {
/* Create a random key */
$keyset  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
$randkey = '';
for ($i=0; $i<$amount; $i++)
$randkey .= substr($keyset, rand(0, strlen($keyset)-1), 1);
}
return $randkey;
}
?>


Dealing with special characters is a real pain in the ass, so I wouldn't even bother with them, especially if you're using the random string to verify something.
Jason Clemons
Former Team Member 2009 - 2012

Matthew K.

Yeah, I saw a function similar to that when I was looking for an existing one, but I decided to write my own with some params.

And definitely, at first I used 'all' for the character set, but after realizing it wasn't a smart idea for the scenario, I switched to using 'alphanumerical', but hey, that's why it's on a param so it can be used however it needs to be :)

MrPhil

Just a comment on generating random keys that an end user may see at some point: so that you don't get complaints about obscenities, blasphemies, or other undesirable words in the key text, consider omitting vowels from the list (a e i o u y). That way, no complete real word will ever be formed.

JBlaze

@MrPhil good point. Never thought of that.
Jason Clemons
Former Team Member 2009 - 2012

Matthew K.

Interesting idea...if I ever have use of this function again, I will most likely add another parameter that would remove vowels from the list of alphabetical characters.

Advertisement: