|
|
Php Random Text Generating - How to Generate Random Text | ||
Discussion by szupie with 13 Replies.
Last Update: June 1, 2010, 1:13 am | |||
QUOTE
<?//author: polmme
$codelenght = 10;
while($newcode_length < $codelenght) {
$x=1;
$y=3;
$part = rand($x,$y);
if($part==1){$a=48;$b=57;} // Numbers
if($part==2){$a=65;$b=90;} // UpperCase
if($part==3){$a=97;$b=122;} // LowerCase
$code_part=chr(rand($a,$
$newcode_length = $newcode_length + 1;
$newcode = $newcode.$code_part;
}
echo $newcode;
?>
I think it's pretty good. If anyone has a better one or suggestions, please tell me.
Fri Feb 4, 2005 Reply New Discussion
Sorry to the author polmme it was a nice example indeed and a lot simpler to understand for beginners than my rewrite I admit.
<?php
for($code_length = 10, $newcode = ''; strlen($newcode) < $code_length; $newcode .= chr(!rand(0, 2) ? rand(48, 57) : (!rand(0, 1) ? rand(65, 90) : rand(97, 122))));
echo $newcode;
?>
I also have one I made based off someone elses code but I lost who that was, anyways here it is. I should have turned it into a function (just did), I know there was more I was going to do, which was randomise the characters within $salt string (just did) as well but didn't continue on this (just did), I might add to it later (just did), I got to be somewhere 5 minutes ago (just got back)
<?php
function mkRandPasswd()
{
define('NUM0', 48);
define('NUM9', 57);
define('LETA', 65);
define('LETZ', 90);
define('LETa', 97);
define('LETz', 122);
$salt = '';
$passwd = '';
define('PASSWD_LEN', 10);
for($nLoop = 0; $nLoop < NUM9 - NUM0 + 1; $nLoop++)
$salt .= chr(mt_rand(NUM0, NUM9));
for($ucLoop = 0; $ucLoop < LETZ - LETA + 1; $ucLoop++)
$salt .= chr(mt_rand(LETA, LETZ));
for($lcLoop = 0; $lcLoop < LETz - LETa + 1; $lcLoop++)
$salt .= chr(mt_rand(LETa, LETz));
$salt = str_shuffle($salt);
for($gen = 0; $gen < PASSWD_LEN; $gen++)
$passwd = $passwd . substr($salt, mt_rand() % strlen($salt), 1);
return array($salt, $passwd);
}
list($salt, $passwd) = mkRandPasswd();
$str_passwd = substr($salt, strlen($salt) / 4, PASSWD_LEN);
echo 'Characters to select for password: ' . $salt . '<br /> The generated password: ' . $passwd . '<br /> The stringed password: ' . $str_passwd;
?>
Remember not much can be said about the script unless the logic is worked out, I still haven't worked mine out, but I can say it's safe enough for password generating, you still would require the user to change the password and not keep it.
Cheers,
MC
Sat Feb 5, 2005 Reply New Discussion
Sat Feb 5, 2005 Reply New Discussion
CODE
for($j = 1; $j <= 30; $j++){
$lim = rand(4,7);
for ($i = 1; $i <= $lim; $i++)
{
echo chr(ord('a') + rand(0,25));
}
echo "<BR>\n";
}
Hope this is useful.
Fri Feb 18, 2005 Reply New Discussion
a,b,c,d,... ,A,B,C,D,... 1,2,3,...
let's say you get a table of 80 fields.
then you could just pick a random field
you could do this in various way: just rand(0,80)
if you have doubts about the randomness of rand, you could rand a random times and then start picking your letters
$i = rand(0,20);
for ($j=0; $j<$i;$j++) rand(0,80);
for ($i=0; $i<$passlenght;$i++)
$pw =$pw.$array[rand(0,80)];
or you could use srand
or you could go like rand(0,846854354)%80
anything that makes you feel safer
Fri Feb 18, 2005 Reply New Discussion
Ya know what you could just do is be sneaky and do this.
$ranNum = rand(100,9999).'-'.Rand(10000,999999);
$hexRan = md5($ranNum );
ThisConverts it to a MD5 Hash and since its random it doesn't reallyMatter. Store it in the DB and you should be good. Of course this codeIs only good if you want to have letters and numbers in the string. IfYou're going for JUST text, well then I would suggest doing somethingLike what is described above.
Tue Sep 8, 2009 Reply New Discussion
CODE
function gen_chars($length = 6) {// Available characters
$chars = '0123456789abcdefghjkmnoprstvwxyz';
$Code = '';
// Generate code
for ($i = 0; $i < $length; ++$i)
{
$Code .= substr($chars, (((int) mt_rand(0,strlen($chars))) - 1),1);
}
return $Code;
}
// Usage
$var = gen_chars(12);
echo $var;
In the function variable $chars = '..'; I did not include all the letters, because I use this function for sending SMS code to activate some feature on a site and some letters does not feet, due to they look similar, like l can look like 1 and etc. if they are one to another, of course if you'll add all the alphabet and maybe even other symbols and even uppercase letters you'll get a much bigger probability for it to be different for example looping it million times and of course making more than 6 or 12 characters/length will have more different variations.
Fri Oct 30, 2009 Reply New Discussion
I want to a making a word from group of the character.Eg. Opinion is a correct word from the group of character ionnipo(generate a group of the word in php from 7 character)
-reply by musavvir
Fri Jan 29, 2010 Reply New Discussion
CODE
$alphanum = "abcdefghijkmnpqrstuvwxyz23456789";$rand = substr(str_shuffle($alphanum), 0, 5); // 5 Being the amount of letters/numbers are select from the variable alphanum
And if you want letters and numbers to have a chance of being repeated:
CODE
$alphanum = "abcdefghijkmnpqrstuvwxyz23456789";$inc = 1;
while ($inc < 5){
$alphanum = $alphanum.'abcdefghijkmnpqrstuvwxyz23456789';
$inc++;
}
$rand = substr(str_shuffle($alphanum), 0, 5); // 5 Being the amount of letters/numbers are select from the variable alphanum
And finally for a random length text string with possible repeated letters or numbers:
CODE
$alphanum = "abcdefghijkmnpqrstuvwxyz23456789";$inc = 1;
while ($inc < 5){
$alphanum = $alphanum.'abcdefghijkmnpqrstuvwxyz23456789';
$inc++;
}
$rand = substr(str_shuffle($alphanum), 0, rand(3,10)); // 3 Being the minimum amound of letters returned and 10 being the maximum
Fri Jan 29, 2010 Reply New Discussion
Sun Feb 21, 2010 Reply New Discussion
abcdef1234567890
where with my method and 8ennett method you can include anything you want, even #$%^ symbols, which can be good for a CAPTCHA or for a SMS code or for any code which needs to be short and have a lot of different variations to not generate the same.
Those long codes which are lets say 16 letters are good for generating confirmation codes, where you don't really care about it, those kind of codes usually are copied and pasted or clicked on.
Tue Feb 23, 2010 Reply New Discussion
Tue Feb 23, 2010 Reply New Discussion
<?php/** * @author Praateek * @copyright 2010 */$randcheck = rand(0, 4); if ($randcheck == 0) { $custom = chr(ord("a") + rand(0, 25)) . Rand(10, 99) . Chr(ord("a") + rand(0, 25)) . rand(10, 99); } elseif ($randcheck == 1) { $custom = rand(10, 99) . Chr(ord("a") + rand(0, 25)) . Rand(10, 99) . Chr(ord("a") + rand(0, 25)); } elseif ($randcheck == 2) { $custom = chr(ord("a") + rand(0, 25)) . Chr(ord("a") + rand(0, 25)) . Rand(10, 99) . rand(10, 99); } elseif ($randcheck == 3) { $custom = rand(10, 99) . Rand(10, 99) . Chr(ord("a") + rand(0, 25)) . Chr(ord("a") + rand(0, 25)); } elseif ($randcheck == 4) { $custom = rand(10, 99) . Chr(ord("a") + rand(0, 25)) . Chr(ord("a") + rand(0, 25)) . rand(10, 99); } echo $custom;?>
Mon May 17, 2010 Reply New Discussion
Tue Jun 1, 2010 Reply New Discussion
Php Sessions I don't really get it... (4)
|
(14) How I Can Install PHP On My PC
|
Index




