Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Generate random string in specified length

This function generates random string up to 32 characters length. It can be used to generate text for captchas or generate random password, when user wants to reset is. It takes length of string to generate as parameter: 

<?php
function random_string($length = 0) 
{   
    $random = md5(uniqid(rand(), true));
    if ($length > 0)
    {
        return substr($random, 0, $length);
    }
    else return $random;
}
echo random_string(0);
?>

You can modify it, by stripping out characters that you don't want to be there, like in some fonts 0 (zero) and O (letter "o") can look similar, so you can add $random = str_replace("O", "", $random); so all string will contain only zeros and no letter o.


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus