PHPBuilder - Generate a random string.



RSS Twitter
Snippets Algorithms

Generate a random string.

by: Matthew Harris
|
October 21, 2005

Version: 0.1

Type: Function

Category: Algorithms

License: GNU General Public License

Description: Generate a random string based on a supplied length and seed. // Generates a 4 character random string, // using the seed 'abcd' $randomStr = getRandomStr(4, 'abcd');



<?php
  function getRandomStr($length = 6, $seed = 'abcdef0123456789')
  {
    for ($str = '', $i = 0; $i < $length; ++$i)
      $str .= $seed{rand(0, strlen($seed) - 1)};

    return $str;
  }
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Matthew Harris

Comment:



Comment:

(Maximum characters: 1200). You have characters left.