时间:2010-07-25 16:03:46

标签: php

6 个答案:

答案 0 :(得分:14)

如果您不想手动输入字母,可以执行以下操作:

<?php
function getRandomWord($len = 10) {
    $word = array_merge(range('a', 'z'), range('A', 'Z'));
    shuffle($word);
    return substr(implode($word), 0, $len);
}

codepad example

如果这是一个数据库ID,我建议你:

function createUniqueId() {
    while (1) {
       $word = getRandomWord();
       if (!idExists($word)) { // idExists returns true if the id is already used
           return $word;
       }
    }
}

答案 1 :(得分:5)

试试这个功能

function randw($length=4){
        return substr(str_shuffle("qwertyuiopasdfghjklzxcvbnm"),0,$length);
    }

答案 2 :(得分:3)

答案 3 :(得分:1)

答案 4 :(得分:0)

<?php 
$letters  = 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" );

function letterfNum($letterNumber ,$resoltNumber){
    global $letters;
        for ($i = 0 ; $i < $resoltNumber ;$i++){
            $lettersTotal = "";
            $rand = array_rand($letters,$letterNumber);
            foreach($rand as $key => $letterIndex){ 
                foreach($letters as $orginalIndex => $orginalValue){
                    if ($letterIndex == $orginalIndex){
                        $lettersTotal .= $orginalValue;
                    }
                }    
            }
        echo $lettersTotal."<br>";  
        }
    }

    letterfNum(5,6);
?>

答案 5 :(得分:0)

您需要使用uniqid

echo uniqid(null, true);