用php显示随机内容


displaying random contents with php

我有一个脚本,它用它以以下格式编写网站url和网站名称:

::<a href='http://master7np.tk>Master7np</a>::<a href='http://master-land.net>
Master-land</a>::<a href='http://nischal.tk>nischal.tk</a>

我想要一个php脚本,在网页中显示这个来自点::的随机url

这是原件,但它不起作用——我的意思是它只显示:

"<a href='http://master7np.tk>Master7np</a>"

但是它应该随机显示(而不仅仅是第一个)。

<?
$xfile = @file("/home/webtraff/public_html/ads.txt");
$random_num = rand (0,count($xfile)-1);
$udata = explode("::",$xfile[$random_num]);
echo "$udata[1]";
?>

您的指令顺序有点错误。我更改了您读取文件的方式。

$file = file_get_contents("/home/webtraff/public_html/ads.txt"); //open the file as string removed the
$udata = explode("::",$file); //then we split by tokens
$udata = array_flip($udata); //change the values to keys
$text = array_rand($udata); //get a random key

将代码更改为以下

<?php
$data = file_get_contents("/home/webtraff/public_html/ads.txt"); //open the file 
$urls = explode("::",$data); //url split by separator
$number=rand(0,count($urls)-1); // Get random number
echo $urls[$number];  //random output

?>