<?php function rand_string($len = 16, $keyword = '') { if (strlen($keyword) > $len) {//关键字不能比总长度长 return false; } $str = ''; $chars = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHIJKMNPQRSTUVWXYZ'; //去掉1跟字母l防混淆 if ($len > strlen($chars)) {//位数过长重复字符串一定次数 $chars = str_repeat($chars, ceil($len / strlen($chars))); } $chars = str_shuffle($chars); //打乱字符串 $str = substr($chars, 0, $len); if (!empty($keyword)) { $start = $len - strlen($keyword); $str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //从随机位置插入关键字 } return $str; } echo rand_string(16,"ab"); //output example:V8bNY6SmkeywordB ?>
登入後複製
?