Home > Article > Backend Development > Implementation method of generating string random code in php_PHP tutorial
php tutorial to generate string random code implementation method
*/
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$index = mt_rand(0, 10);
echo $index;
//Look at a more complicated one
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$validcharnumber = strlen($validcharacters);
$index = mt_rand(0, $validcharnumber-1);
$randomcharacter = $validcharacters[$index];
echo $randomcharacter;
//Example 2
$length = 5;
$result = "";
for ($i = 0; $i < $length; $i++) {
$index = mt_rand(0, $validcharnumber-1);
$result .= $validcharacters[$index];
}
//
function getrandomstring($length = 6) {
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$validcharnumber = strlen($validcharacters);
$result = "";
for ($i = 0; $i < $length; $i++) {
$index = mt_rand(0, $validcharnumber - 1); $index
$result .= $validcharacters[$index];
}
return $result;
}
echo getrandomstring();