Generate random strings. #strings #generator #php
<?php
/**
* Generate random strings.
*
* @author JoseRobinson.com
* @link GitHup: https://gist.github.com/5324883
* @version 201304062359
* @param int $len
* @param string $chars (optional)
* @return string
*/
function generate_string($len, $chars = 'abcdefghijklmnopqrstuvwxyz0987654321')
{
$line = '';
for ($i = 1; $i <= $len; $i++)
{
$line .= $chars[mt_rand(0, (strlen($chars)-1))];
}
return $line;
}
Generate a random string with the specified length. Optionally you can set the chars to use for generate the string.
require 'generate_string.php';
// Normal usage:
printf('<p>Random string: %s</p>', generate_string('5'));
// Generating a password
printf('<p>Password: %s</p>', generate_string('8', '!@#$%:;=()-_/.?=abcdefghkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ0987654321!@#$%:;=()-_/.?='));