0

Hello Every one I have a problem in my website that i want to generate the user ids like 0u3012ewd0 as we see on many sites but don't have any idea how to do this.

Please help me in this problem

Thanks alot.

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by architact
1

How I would do it

<?
$stringlen = 15;
$randvarray = str_split("abcdefghijklmnopqrstuvwxyz0123456789");
$stringid = "";

for($i = 0; $i < $stringlen; $i++)
{
	$value = rand(0, count($randvarray) - 1);
	$stringid .= $randvarray[$value];
}

echo $stringid;
?>
Votes + Comments
Great Post
0

Thought I'd add another example:

/**
* Returns a random Alpha-Numeric string
 * @param Int $len Length of string to return
 * @return Str
 */
function randAlpha($len = 15) {
	$str = '';
	for($i = 0; $i < $len; $i++) {
		$rand = rand()%36;
		$str .= chr($rand > 10 ? $rand%26+97 : $rand%10+48);
	}
	return $str;
}

The resulting alpha-numeric chars won't be as evenly distributed as R0bb0b's example since rand() will use the range of 0-RAND_MAX, but just another way at it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.