php随机评论生成器

时间:2012-07-07 12:36:41

标签: php random comments chat

这个看起来有点奇怪,但有没有任何脚本会产生随机评论,有点像聊天室评论。

随机的东西,如“我很无聊”,“嘿人”,“要记录cya”,“任何人都看过电影 _ ”。

有人遇到过这样的事吗?

3 个答案:

答案 0 :(得分:3)

$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);

答案 1 :(得分:3)

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

一个用于personal pronoun,一个用于操作/动词......

$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)]
}

答案 2 :(得分:1)

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

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