2

Hello i want to generate random string in php with repetition like Ag7hlA. is there anyway to do it i am trying this

<?php
function random_string($length)
{
    $length = (int) $length;
    if ($length < 1) return '';

    $base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
    $max = strlen($base) - 1;

    $string = '';    
    while ($len--)
    {
        $string = $base[mt_rand(0, $max)];
    }
    return $string;
}    
?>

But the random string is not being generated

user2340767
  • 527
  • 1
  • 6
  • 10
  • 4
    Follow the link (Same problem) of Stack overflow : http://stackoverflow.com/questions/4356289/php-random-string-generator?rq=1 – Vaibhav Jain May 06 '13 at 05:47

1 Answers1

0
function generateRandomString($length = 10) {
    $char = 'abcdefghijklmnopqrstuvwxyz';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $char[rand(0, strlen($char) - 1)];
    }
    return $randomString;
}

if u want add numbers too, than the $char will be--->

$char = 'abcdefghijklmnopqrstuvwxyz1234567890';

and if capital cases--->

$char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
Ashok Damani
  • 3,896
  • 4
  • 30
  • 48