繁体   English   中英

PHP随机注释生成器

[英]php random comment generator

这似乎有些奇怪,但是是否有任何脚本在生成随机注释,就像聊天室注释一样。

诸如“我很无聊”,“嘿人”,“将要结识cya”,“任何人看过电影_ ”之类的随机材料。

有人遇到过这样的事情吗?

$comments = array("Im bored", "Hey people", "Gonna log cya", "Anyone seen the film");
$random_comment = array_rand($comments);

echo $comments[$random_comment];


如果您有一个带有注释的MySQL表,则可以执行以下操作:

 $result = mysql_query("SELECT `comment` FROM `comments` ORDER BY RAND() LIMIT 0,1"); if($result) echo mysql_result($result, 0); 

您可以通过组合来自两个数组的随机选择来生成随机注释。

一个用于人称代词 ,另一个用于动作/动词...

$pronoun = array(
 "I'm",
 "You're"
 "He's",
 "She's",
 "They're"
);

$action = array(
 "stacking",
 "overflowing",
 "confused",
 "bewildered",
 "wondering how many more of these I can make up",
 "getting bored... So that's enough for now..."
);

在每个数组上运行一次array_rand()将返回一个随机索引,并连接相应的值将生成一个注释。 您必须增强阵列,使其适合您的需求。

$comment = $pronoun[array_rand($pronoun)] . ' ' . $action[array_rand($action)];

创建评论生成器功能将进一步简化使用该系统的过程-

function generateComment(){
  global $pronoun,$action;
  return $pronoun[array_rand($pronoun)] . ' ' . $action[array_rand($action)]
}
$comments = array("Im bored", "Hey people", "Gonna log cya", "Anyone seen the film");
shuffle($comments);

echo $comments[0];//1,2,3.....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM