yii\base\Security::generateRandomString PHP Method

generateRandomString() public method

The string generated matches [A-Za-z0-9_-]+ and is transparent to URL-encoding.
public generateRandomString ( integer $length = 32 ) : string
$length integer the length of the key in characters
return string the generated random key
    public function generateRandomString($length = 32)
    {
        if (!is_int($length)) {
            throw new InvalidParamException('First parameter ($length) must be an integer');
        }
        if ($length < 1) {
            throw new InvalidParamException('First parameter ($length) must be greater than 0');
        }
        $bytes = $this->generateRandomKey($length);
        // '=' character(s) returned by base64_encode() are always discarded because
        // they are guaranteed to be after position $length in the base64_encode() output.
        return strtr(substr(base64_encode($bytes), 0, $length), '+/', '_-');
    }

Usage Example

Example #1
1
 /**
  * @param int $length
  * @return string
  */
 public static function generateSecret($length = 20)
 {
     $security = new Security();
     $full = Base32::encode($security->generateRandomString($length));
     return substr($full, 0, $length);
 }
All Usage Examples Of yii\base\Security::generateRandomString