php教程生成字符串隨機碼實現方法
*/
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$index = mt_rand(0, 10);
echo $index;
//看一款復雜一點的
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$validcharnumber = strlen($validcharacters);
$index = mt_rand(0, $validcharnumber-1);
$randomcharacter = $validcharacters[$index];
echo $randomcharacter;
//實例二
$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);
$result .= $validcharacters[$index];
}
return $result;
}
echo getrandomstring();