Can anyone help me with a solution that pulls the position and value of a random character from a given string using PHP. For example I have a a string variable $string = \
For anyone who might want to use it for the same case like mine:
I just wrote a simple SecretGenerator:
echo generateSecret(10);
function generateSecret($length) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&!^§\$ß";
$key = "";
for($i = 0; $i <= $length; $i++){
$key .= $chars[mt_rand(0, strlen($chars)-1)];
}
return $key;
}
Will return something like this: "6qß^0UoH7NP"
Of course, this is not a secure function, but for generating simple Hashes it's quite okay to use.
Just for fun:
$chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
echo $string[array_rand($chars)];
$string = 'helloworld';
$pos = rand(0,(strlen($string)-1));
echo $pos."th char is: ". $string[$pos];