如何在 PHP 中生成随机单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3329903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:21:47 来源:igfitidea点击:
How can I generate random words in PHP?
提问by ABC Girl
How I can get random words without repeating in PHP? Help
如何在不重复 PHP 的情况下获得随机单词?帮助
回答by Benjamin Crouzier
If you don't want to type the alphabet manually, you can do the folowing:
如果您不想手动输入字母,可以执行以下操作:
<?php
function getRandomWord($len = 10) {
$word = array_merge(range('a', 'z'), range('A', 'Z'));
shuffle($word);
return substr(implode($word), 0, $len);
}
If this is a database id, i suggest you the folowing:
如果这是一个数据库 ID,我建议您使用以下方法:
function createUniqueId() {
while (1) {
$word = getRandomWord();
if (!idExists($word)) { // idExists returns true if the id is already used
return $word;
}
}
}
回答by Arthur Veselov
Try this func
试试这个功能
function randw($length=4){
return substr(str_shuffle("qwertyuiopasdfghjklzxcvbnm"),0,$length);
}
回答by karim79
$words = preg_split('//', 'abcdefghijklmnopqrstuvwxyz0123456789', -1);
shuffle($words);
foreach($words as $word) {
echo $word . '<br />';
}
回答by conlippert
function getrandomstring($length) {
global $template;
settype($template, "string");
$template = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* this line can include numbers or not */
settype($length, "integer");
settype($rndstring, "string");
settype($a, "integer");
settype($b, "integer");
for ($a = 0; $a <= $length; $a++) {
$b = rand(0, strlen($template) - 1);
$rndstring .= $template[$b];
}
return $rndstring;
}
回答by ????? ?????
<?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);
?>
回答by Hyman
All you need is to use uniqid.
您只需要使用uniqid.
echo uniqid(null, true);

