Toolbox::getRandomString PHP Méthode

getRandomString() static public méthode

Get a random string
static public getRandomString ( integer $length, boolean $high = false ) : random
$length integer of the random string
$high boolean strength of the random source (since 9.2)
Résultat random string
    static function getRandomString($length, $high = false)
    {
        $factory = new RandomLib\Factory();
        if ($high) {
            /* Notice "High" imply mcrypt extension, unwanted for now
               See https://github.com/ircmaxell/RandomLib/issues/57 */
            $generator = $factory->getMediumStrengthGenerator();
        } else {
            $generator = $factory->getLowStrengthGenerator();
        }
        return $generator->generateString($length, RandomLib\Generator::CHAR_LOWER + RandomLib\Generator::CHAR_DIGITS);
    }

Usage Example

Exemple #1
0
 /**
  * @covers Toolbox::getRandomString
  */
 public function testGetRandomString()
 {
     for ($len = 20; $len < 50; $len += 5) {
         // Low strength
         $str = Toolbox::getRandomString($len);
         $this->assertEquals($len, strlen($str));
         $this->assertTrue(ctype_alnum($str));
         // High strength
         $str = Toolbox::getRandomString($len, true);
         $this->assertEquals($len, strlen($str));
         $this->assertTrue(ctype_alnum($str));
     }
 }
All Usage Examples Of Toolbox::getRandomString