代码很精简,如下。
- //随机从file.txt文本文件中读取一行出来
- $file = "./www.blogguy.cn.txt"; //文件位置;
- //是否存在
- if(file_exists($file)){
- $data = file($file); //将文件存放在一个数组中;
- $num = count($data); //条数;
- $id = mt_rand(0,$num-1); //随机数字;
- $blogguy.cn_text = chop($data[$id]); //显示第几行数据,并去除空格;
- }
- header("Content-Type: text/html; charset=gb2312");
- echo($blogguy.cn_text);
- ?>