[英]How to load random URL from a file in php
I am trying to create news portal. 我正在尝试创建新闻门户。 and I want to load random news link which is in the list.txt file and created a php script which calls the list.txt and load the link from the list. 我想加载list.txt文件中的随机新闻链接,并创建一个调用list.txt的php脚本,并从列表中加载链接。
checkout my codes. 签出我的代码。
script.php script.php
<?php
$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);
// Here is our random link URL
$picked = $loadlist[$rand];
?>
<meta http-equiv="refresh" content="2;url=<?php echo $picked; ?>">
list.txt list.txt
http://news.com/news1.html
http://news.com/news2.html
http://news.com/news3.html
http://news.com/news4.html
http://news.com/news5.html
Above code is working fine now, thanks 上面的代码现在可以正常工作了,谢谢
Get a random integer based on item count of your $loadlist
array. 根据$loadlist
数组的项目计数获取随机整数。
$loadlist = explode("\n", file_get_contents('list.txt'));
$rand = rand(0,count($loadlist)-1);
// Here is our random link URL
$picked = $loadlist[$rand];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.