名站导航为爱好php程序的朋友们提供php相关的教程知识。
很实用的3个PhP程序随机字符串函数生成器
具体代码如下如下: <?php 函数1: /* * 产生随机字符串 * * 产生一个指定长度的随机字符串,并返回给用户 * * @access public * @param int $len 产生字符串的位数 * @return string */ function randStr($len) { $chars="ABCDEFGhIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz-=/?~!@#$%^&*()"; // characters to build the password from $string=""; for(;$len >= 1;$len--) { $position=rand()%strlen($chars); $string.=substr($chars,$position,1); } return $string; } 函数2: function generate_password( $length = 8 ) { // 密码字符集,可任意添加你需要的字符 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGhIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}~` =,.;:/?'; $password = ''; //for ( $i = 0; $i < $length; $i ) while (strlen($password) < $length) { // 这里提供两种字符获取方式 // 第一种是使用 substr 截取$chars中的任意一位字符; // 第二种是取字符数组 $chars 的任意元素 // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); $password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; } return $password; } 函数3: function genRandomString($len) { $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "h", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "[", "]", "{", "}", "~", "`", " ", "=", ",", ".", ":", ";", "/", "?" ); $charsLen = count($chars) - 1; shuffle($chars); // 将数组打乱 $output = ""; for ($i=0; $i<$len; $i ) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } echo "第一个:".randStr(50)."<br />"; echo "第二个:".generate_password(50)."<br />"; echo "第三个:".genRandomString(50)."<br />"; ?> 产生的随机字符串分别为:(www.mzdh.net 网站建设) 第一个:1A5jOOJmM9wzMVFSJr5Utk)kpU^YmSN(Tig5unC~FyoRDt8K3? 第二个:e/$g[UE@Qm*tAsvNcGKbyfCoo,@?NcL0%) Kkao-m]jtGxvWik 第三个:_}vz8 PIjkf]2Z*]^C?^h*a7V5thbSXKwuO)R4?I^_vC4G;Lp[ 名站网址导航好了关于php程序的知识就说到这里希望可以帮助需要的朋友。,具体实例操作学习PhP程序操作XML的类DOMDocument